Skip to content

Commit

Permalink
Improve @safe in doc.d
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored and dlang-bot committed Aug 20, 2022
1 parent 9df6746 commit a681b65
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
62 changes: 31 additions & 31 deletions compiler/src/dmd/doc.d
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ private final class ParamSection : Section
buf.writestring("$(DDOC_PARAM_ID ");
{
size_t o = buf.length;
Parameter fparam = isFunctionParameter(a, namestart, namelen);
Parameter fparam = isFunctionParameter(a, namestart[0 .. namelen]);
if (!fparam)
{
// Comments on a template might refer to function parameters within.
// Search the parameters of nested eponymous functions (with the same name.)
fparam = isEponymousFunctionParameter(a, namestart, namelen);
fparam = isEponymousFunctionParameter(a, namestart[0 .. namelen]);
}
bool isCVariadic = isCVariadicParameter(a, namestart[0 .. namelen]);
if (isCVariadic)
Expand Down Expand Up @@ -328,7 +328,7 @@ private final class MacroSection : Section
private alias Sections = Array!(Section);

// Workaround for missing Parameter instance for variadic params. (it's unnecessary to instantiate one).
private bool isCVariadicParameter(Dsymbols* a, const(char)[] p)
private bool isCVariadicParameter(Dsymbols* a, const(char)[] p) @safe
{
foreach (member; *a)
{
Expand All @@ -339,7 +339,7 @@ private bool isCVariadicParameter(Dsymbols* a, const(char)[] p)
return false;
}

private Dsymbol getEponymousMember(TemplateDeclaration td)
private Dsymbol getEponymousMember(TemplateDeclaration td) @safe
{
if (!td.onemember)
return null;
Expand Down Expand Up @@ -2565,17 +2565,17 @@ private size_t replaceMarkdownEmphasis(ref OutBuffer buf, const ref Loc loc, ref

/****************************************************
*/
private bool isIdentifier(Dsymbols* a, const(char)* p, size_t len)
private bool isIdentifier(Dsymbols* a, const(char)[] s)
{
foreach (member; *a)
{
if (auto imp = member.isImport())
{
// For example: `public import str = core.stdc.string;`
// This checks if `p` is equal to `str`
// This checks if `s` is equal to `str`
if (imp.aliasId)
{
if (p[0 .. len] == imp.aliasId.toString())
if (s == imp.aliasId.toString())
return true;
}
else
Expand All @@ -2590,14 +2590,14 @@ private bool isIdentifier(Dsymbols* a, const(char)* p, size_t len)
}
fullyQualifiedImport ~= imp.id.toString();

// Check if `p` == `core.stdc.string`
if (p[0 .. len] == fullyQualifiedImport)
// Check if `s` == `core.stdc.string`
if (s == fullyQualifiedImport)
return true;
}
}
else if (member.ident)
{
if (p[0 .. len] == member.ident.toString())
if (s == member.ident.toString())
return true;
}

Expand All @@ -2607,20 +2607,20 @@ private bool isIdentifier(Dsymbols* a, const(char)* p, size_t len)

/****************************************************
*/
private bool isKeyword(const(char)* p, size_t len)
private bool isKeyword(const(char)[] str) @safe
{
immutable string[3] table = ["true", "false", "null"];
foreach (s; table)
{
if (p[0 .. len] == s)
if (str == s)
return true;
}
return false;
}

/****************************************************
*/
private TypeFunction isTypeFunction(Dsymbol s)
private TypeFunction isTypeFunction(Dsymbol s) @safe
{
FuncDeclaration f = s.isFuncDeclaration();
/* f.type may be NULL for template members.
Expand All @@ -2636,14 +2636,14 @@ private TypeFunction isTypeFunction(Dsymbol s)

/****************************************************
*/
private Parameter isFunctionParameter(Dsymbol s, const(char)* p, size_t len)
private Parameter isFunctionParameter(Dsymbol s, const(char)[] str) @safe
{
TypeFunction tf = isTypeFunction(s);
if (tf && tf.parameterList.parameters)
{
foreach (fparam; *tf.parameterList.parameters)
{
if (fparam.ident && p[0 .. len] == fparam.ident.toString())
if (fparam.ident && str == fparam.ident.toString())
{
return fparam;
}
Expand All @@ -2654,11 +2654,11 @@ private Parameter isFunctionParameter(Dsymbol s, const(char)* p, size_t len)

/****************************************************
*/
private Parameter isFunctionParameter(Dsymbols* a, const(char)* p, size_t len)
private Parameter isFunctionParameter(Dsymbols* a, const(char)[] p) @safe
{
for (size_t i = 0; i < a.dim; i++)
foreach (Dsymbol sym; *a)
{
Parameter fparam = isFunctionParameter((*a)[i], p, len);
Parameter fparam = isFunctionParameter(sym, p);
if (fparam)
{
return fparam;
Expand All @@ -2669,11 +2669,11 @@ private Parameter isFunctionParameter(Dsymbols* a, const(char)* p, size_t len)

/****************************************************
*/
private Parameter isEponymousFunctionParameter(Dsymbols *a, const(char) *p, size_t len)
private Parameter isEponymousFunctionParameter(Dsymbols *a, const(char)[] p) @safe
{
for (size_t i = 0; i < a.dim; i++)
foreach (Dsymbol dsym; *a)
{
TemplateDeclaration td = (*a)[i].isTemplateDeclaration();
TemplateDeclaration td = dsym.isTemplateDeclaration();
if (td && td.onemember)
{
/* Case 1: we refer to a template declaration inside the template
Expand All @@ -2692,7 +2692,7 @@ private Parameter isEponymousFunctionParameter(Dsymbols *a, const(char) *p, size
/// ...ddoc...
alias case2 = case1!int;
*/
AliasDeclaration ad = (*a)[i].isAliasDeclaration();
AliasDeclaration ad = dsym.isAliasDeclaration();
if (ad && ad.aliassym)
{
td = ad.aliassym.isTemplateDeclaration();
Expand All @@ -2703,7 +2703,7 @@ private Parameter isEponymousFunctionParameter(Dsymbols *a, const(char) *p, size
Dsymbol sym = getEponymousMember(td);
if (sym)
{
Parameter fparam = isFunctionParameter(sym, p, len);
Parameter fparam = isFunctionParameter(sym, p);
if (fparam)
{
return fparam;
Expand Down Expand Up @@ -4960,17 +4960,17 @@ private void highlightText(Scope* sc, Dsymbols* a, Loc loc, ref OutBuffer buf, s
i = buf.bracket(i, "$(DDOC_AUTO_PSYMBOL_SUPPRESS ", j - 1, ")") - 1;
break;
}
if (isIdentifier(a, start, len))
if (isIdentifier(a, start[0 .. len]))
{
i = buf.bracket(i, "$(DDOC_AUTO_PSYMBOL ", j, ")") - 1;
break;
}
if (isKeyword(start, len))
if (isKeyword(start[0 .. len]))
{
i = buf.bracket(i, "$(DDOC_AUTO_KEYWORD ", j, ")") - 1;
break;
}
if (isFunctionParameter(a, start, len))
if (isFunctionParameter(a, start[0 .. len]))
{
//printf("highlighting arg '%s', i = %d, j = %d\n", arg.ident.toChars(), i, j);
i = buf.bracket(i, "$(DDOC_AUTO_PARAM ", j, ")") - 1;
Expand Down Expand Up @@ -5059,7 +5059,7 @@ private void highlightCode(Scope* sc, Dsymbols* a, ref OutBuffer buf, size_t off
if (i < j)
{
size_t len = j - i;
if (isIdentifier(a, start, len))
if (isIdentifier(a, start[0 .. len]))
{
i = buf.bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
continue;
Expand All @@ -5070,12 +5070,12 @@ private void highlightCode(Scope* sc, Dsymbols* a, ref OutBuffer buf, size_t off
if (i < j)
{
size_t len = j - i;
if (isIdentifier(a, start, len))
if (isIdentifier(a, start[0 .. len]))
{
i = buf.bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
continue;
}
if (isFunctionParameter(a, start, len))
if (isFunctionParameter(a, start[0 .. len]))
{
//printf("highlighting arg '%s', i = %d, j = %d\n", arg.ident.toChars(), i, j);
i = buf.bracket(i, "$(DDOC_PARAM ", j, ")") - 1;
Expand Down Expand Up @@ -5199,12 +5199,12 @@ private void highlightCode2(Scope* sc, Dsymbols* a, ref OutBuffer buf, size_t of
if (!sc)
break;
size_t len = lex.p - tok.ptr;
if (isIdentifier(a, tok.ptr, len))
if (isIdentifier(a, tok.ptr[0 .. len]))
{
highlight = "$(D_PSYMBOL ";
break;
}
if (isFunctionParameter(a, tok.ptr, len))
if (isFunctionParameter(a, tok.ptr[0 .. len]))
{
//printf("highlighting arg '%s', i = %d, j = %d\n", arg.ident.toChars(), i, j);
highlight = "$(D_PARAM ";
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/identifier.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ nothrow:
return name.ptr;
}

extern (D) override const(char)[] toString() const pure
extern (D) override const(char)[] toString() const pure @safe
{
return name;
}
Expand Down

0 comments on commit a681b65

Please sign in to comment.