Skip to content

Commit

Permalink
Refactor use of Loc/SourceLoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jan 26, 2025
1 parent b2c9da1 commit e816c73
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 74 deletions.
14 changes: 5 additions & 9 deletions compiler/src/dmd/doc.d
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ void gendocfile(Module m, const char[] ddoctext, const char* datetime, ErrorSink
OutBuffer buf;
if (m.filetype == FileType.ddoc)
{
const ploc = m.md ? &m.md.loc : &m.loc;
Loc loc = *ploc;
if (!loc.filename)
loc.filename = srcfilename.ptr;
Loc loc = m.md ? m.md.loc : m.loc;

size_t commentlen = m.comment ? strlen(cast(char*)m.comment) : 0;
Dsymbols a;
Expand Down Expand Up @@ -3603,7 +3600,7 @@ struct MarkdownLinkReferences
auto id = Identifier.lookup(ids[0].ptr, ids[0].length);
if (id)
{
auto loc = Loc();
auto loc = Loc.initial;
Dsymbol pscopesym;
auto symbol = _scope.search(loc, id, pscopesym, SearchOpt.ignoreErrors);
for (size_t i = 1; symbol && i < ids.length; ++i)
Expand Down Expand Up @@ -4096,9 +4093,8 @@ size_t endRowAndTable(ref OutBuffer buf, size_t iStart, size_t iEnd, ref Markdow
*/
void highlightText(Scope* sc, Dsymbols* a, Loc loc, ref OutBuffer buf, size_t offset)
{
const incrementLoc = loc.linnum == 0 ? 1 : 0;
loc.linnum = loc.linnum + incrementLoc;
loc.charnum = 0;
loc.nextLine();

//printf("highlightText()\n");
bool leadingBlank = true;
size_t iParagraphStart = offset;
Expand Down Expand Up @@ -4202,7 +4198,7 @@ void highlightText(Scope* sc, Dsymbols* a, Loc loc, ref OutBuffer buf, size_t of
lineQuoted = false;
tableRowDetected = false;
iLineStart = i + 1;
loc.linnum = loc.linnum + incrementLoc;
loc.nextLine();

// update the paragraph start if we just entered a macro
if (previousMacroLevel < macroLevel && iParagraphStart < iLineStart)
Expand Down
8 changes: 2 additions & 6 deletions compiler/src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ else
* see verrorReport for arguments
* Returns: true if error handling is done, false to continue printing to stderr
*/
alias DiagnosticHandler = bool delegate(const ref Loc location, Color headerColor, const(char)* header, const(char)* messageFormat, va_list args, const(char)* prefix1, const(char)* prefix2);
alias DiagnosticHandler = bool delegate(const ref SourceLoc location, Color headerColor, const(char)* header, const(char)* messageFormat, va_list args, const(char)* prefix1, const(char)* prefix2);

/**
* The diagnostic handler.
Expand Down Expand Up @@ -671,11 +671,7 @@ private void verrorPrint(const(char)* format, va_list ap, ref ErrorInfo info)

if (diagnosticHandler !is null)
{
Loc diagLoc;
diagLoc.linnum = info.loc.line;
diagLoc.charnum = info.loc.charnum;
diagLoc.filename = (info.loc.filename ~ '\0').ptr;
if (diagnosticHandler(diagLoc, info.headerColor, header, format, ap, info.p1, info.p2))
if (diagnosticHandler(info.loc, info.headerColor, header, format, ap, info.p1, info.p2))
return;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,7 @@ private bool checkNogc(FuncDeclaration f, ref Loc loc, Scope* sc)
if (isRootTraitsCompilesScope(sc) ? !sc.func.isNogcBypassingInference() : !sc.func.setGCCall(f))
return false;

if (loc.linnum == 0) // e.g. implicitly generated dtor
if (loc == Loc.initial) // e.g. implicitly generated dtor
loc = sc.func.loc;

// Lowered non-@nogc'd hooks will print their own error message inside of nogc.d (NOGCVisitor.visit(CallExp e)),
Expand Down
26 changes: 13 additions & 13 deletions compiler/src/dmd/frontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ nothrow:
diagnosticHandler = prevHandler;
}

bool diagHandler(const ref Loc loc, Color headerColor, const(char)* header,
bool diagHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
import core.stdc.string;
Expand Down Expand Up @@ -529,7 +529,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool error(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool error(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about an error message.
Expand All @@ -543,7 +543,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool errorSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool errorSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports a warning message.
Expand All @@ -557,7 +557,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool warning(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool warning(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about a warning message.
Expand All @@ -571,7 +571,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool warningSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool warningSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports a deprecation message.
Expand All @@ -585,7 +585,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool deprecation(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool deprecation(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about a deprecation message.
Expand All @@ -599,7 +599,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool deprecationSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool deprecationSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
}

/**
Expand Down Expand Up @@ -644,29 +644,29 @@ nothrow:
return deprecationCount_;
}

override bool error(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool error(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
errorCount_++;
return false;
}

override bool errorSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool errorSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}

override bool warning(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool warning(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
warningCount_++;
return false;
}

override bool warningSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool warningSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}

override bool deprecation(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool deprecation(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
if (useDeprecated == DiagnosticReporting.error)
errorCount_++;
Expand All @@ -675,7 +675,7 @@ nothrow:
return false;
}

override bool deprecationSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool deprecationSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/src/dmd/iasmdmd.d
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,8 @@ code *asm_emit(Loc loc,

if (driverParams.symdebug)
{
cdb.genlinnum(Srcpos.create(loc.filename, loc.linnum, loc.charnum));
SourceLoc sl = SourceLoc(loc);
cdb.genlinnum(Srcpos.create(sl.filename.ptr, sl.linnum, sl.charnum));
}

cdb.append(pc);
Expand Down Expand Up @@ -3682,7 +3683,10 @@ code *asm_db_parse(OP *pop)
CodeBuilder cdb;
cdb.ctor();
if (driverParams.symdebug)
cdb.genlinnum(Srcpos.create(asmstate.loc.filename, asmstate.loc.linnum, asmstate.loc.charnum));
{
SourceLoc sl = SourceLoc(asmstate.loc);
cdb.genlinnum(Srcpos.create(sl.filename.ptr, sl.linnum, sl.charnum));
}
cdb.genasm(bytes.peekSlice());
code *c = cdb.finish();

Expand Down
19 changes: 9 additions & 10 deletions compiler/src/dmd/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,18 @@ public:
{
if (loc.isValid())
{
if (auto filename = loc.filename.toDString)
SourceLoc sl = SourceLoc(loc);
if (sl.filename.length > 0 && sl.filename != this.filename)
{
if (filename != this.filename)
{
this.filename = filename;
property("file", filename);
}
this.filename = sl.filename;
property("file", sl.filename);
}
if (loc.linnum)

if (sl.linnum)
{
property(linename, loc.linnum);
if (loc.charnum)
property(charname, loc.charnum);
property(linename, sl.linnum);
if (sl.charnum)
property(charname, sl.charnum);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dmd/location.d
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ nothrow:
return _linnum = num;
}

/// Advance this location to the first column of the next line
void nextLine()
{
if (this._linnum)
{
this._linnum++;
this.charnum = 0;
}
}

/***
* Returns: filename for this location, null if none
*/
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -5379,7 +5379,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
*/
private void checkDanglingElse(Loc elseloc)
{
if (token.value != TOK.else_ && token.value != TOK.catch_ && token.value != TOK.finally_ && lookingForElse.linnum != 0)
if (token.value != TOK.else_ && token.value != TOK.catch_ && token.value != TOK.finally_ && lookingForElse.isValid)
{
eSink.warning(elseloc, "else is dangling, add { } after condition at %s", lookingForElse.toChars());
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dmd/s2ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,8 @@ private void block_setLoc(block *b, const ref Loc loc) nothrow

private void srcpos_setLoc(ref Srcpos s, const ref Loc loc) nothrow
{
s.set(loc.filename, loc.linnum, loc.charnum);
SourceLoc sl = SourceLoc(loc);
s.set(sl.filename.ptr, sl.line, sl.column);
}

private bool isAssertFalse(const Expression e) nothrow
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dmd/tocsym.d
Original file line number Diff line number Diff line change
Expand Up @@ -882,5 +882,6 @@ Symbol *toSymbol(Type t)
*/
Srcpos toSrcpos(Loc loc)
{
return Srcpos.create(loc.filename, loc.linnum, loc.charnum);
SourceLoc sl = SourceLoc(loc);
return Srcpos.create(sl.filename.ptr, sl.line, sl.column);
}
2 changes: 1 addition & 1 deletion compiler/test/dub_package/retrieve_scope.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main()
global.path.push((dmdParentDir ~ "/druntime/import" ~ '\0').ptr);

// comment for error output in parsing & semantic
diagnosticHandler = (const ref Loc location,
diagnosticHandler = (const ref SourceLoc location,
Color headerColor,
const(char)* header,
const(char)* messageFormat,
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/unit/frontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ unittest

string[] diagnosticMessages;

nothrow bool diagnosticHandler(const ref Loc loc, Color headerColor, const(char)* header,
nothrow bool diagnosticHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
OutBuffer tmp;
Expand Down Expand Up @@ -251,7 +251,7 @@ unittest

string[] diagnosticMessages;

nothrow bool diagnosticHandler(const ref Loc loc, Color headerColor, const(char)* header,
nothrow bool diagnosticHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
OutBuffer tmp;
Expand Down Expand Up @@ -368,7 +368,7 @@ unittest

string[] diagnosticMessages;

nothrow bool diagnosticHandler(const ref Loc loc, Color headerColor, const(char)* header,
nothrow bool diagnosticHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
OutBuffer tmp;
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/unit/interfaces/check_implementations_20861.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import support : afterEach, beforeEach, compiles, stripDelimited, Diagnostic;
}.stripDelimited;

enum message = "Error: class test.Bar interface function void foo() is not implemented";
const expected = Diagnostic(Loc(filename, 6, 1), message);
const expected = Diagnostic(SourceLoc(filename, 6, 1), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand All @@ -67,7 +67,7 @@ import support : afterEach, beforeEach, compiles, stripDelimited, Diagnostic;
}.stripDelimited;

enum message = "Error: class test.Bar interface function void foo() is not implemented";
const expected = Diagnostic(Loc(filename, 6, 1), message);
const expected = Diagnostic(SourceLoc(filename, 6, 1), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand All @@ -94,7 +94,7 @@ import support : afterEach, beforeEach, compiles, stripDelimited, Diagnostic;
}.stripDelimited;

enum message = "Error: class test.Bar interface function void foo() is not implemented";
const expected = Diagnostic(Loc(filename, 11, 1), message);
const expected = Diagnostic(SourceLoc(filename, 11, 1), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand Down Expand Up @@ -123,7 +123,7 @@ import support : afterEach, beforeEach, compiles, stripDelimited, Diagnostic;
}.stripDelimited;

enum message = "Error: class test.B interface function void foo() is not implemented";
const expected = Diagnostic(Loc(filename, 11, 1), message);
const expected = Diagnostic(SourceLoc(filename, 11, 1), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/unit/lexer/diagnostic_reporter.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unittest
{
int errorCount;

override bool error(const ref Loc, const(char)*, va_list, const(char)*, const(char)*)
override bool error(const ref SourceLoc, const(char)*, va_list, const(char)*, const(char)*)
{
errorCount++;
return true;
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/unit/lexer/lexer_dmdlib.d
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ unittest
import dmd.errors;

const(char)[][2][] diagnosticMessages;
nothrow bool diagnosticHandler(const ref Loc loc, Color headerColor, const(char)* header,
nothrow bool diagnosticHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
OutBuffer tmp;
Expand Down
4 changes: 2 additions & 2 deletions compiler/test/unit/objc/protocols/diagnostic_messages.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ unittest
}.stripDelimited;

enum message = "Error: class test.Bar interface function extern (Objective-C) static void foo() is not implemented";
auto expected = Diagnostic(Loc(filename, 8, 1), message);
auto expected = Diagnostic(SourceLoc(filename, 8, 1), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand All @@ -65,7 +65,7 @@ unittest
}.stripDelimited;

enum message = "Error: function test.Foo.foo function body only allowed in final functions in interface Foo";
auto expected = Diagnostic(Loc(filename, 4, 17), message);
auto expected = Diagnostic(SourceLoc(filename, 4, 17), message);

const diagnostics = compiles(code, filename);
assert(diagnostics == [expected], "\n" ~ diagnostics.toString);
Expand Down
Loading

0 comments on commit e816c73

Please sign in to comment.