Refactor use of Loc/SourceLoc (#20785)

This commit is contained in:
Dennis 2025-01-26 14:06:14 +01:00 committed by GitHub
parent b2c9da1337
commit b289b6a6dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 101 additions and 80 deletions

View file

@ -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;
@ -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)
@ -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;
@ -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)

View file

@ -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.
@ -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;
}

View file

@ -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)),

View file

@ -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;
@ -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.
@ -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.
@ -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.
@ -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.
@ -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.
@ -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);
}
/**
@ -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_++;
@ -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;
}

View file

@ -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);
@ -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();

View file

@ -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);
}
}
}

View file

@ -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
*/
@ -126,9 +136,7 @@ nothrow:
bool showColumns = Loc.showColumns,
MessageStyle messageStyle = Loc.messageStyle) const nothrow
{
OutBuffer buf;
writeSourceLoc(buf, SourceLoc(this), showColumns, messageStyle);
return buf.extractChars();
return SourceLoc(this).toChars(showColumns, messageStyle);
}
/**
@ -140,9 +148,11 @@ nothrow:
*/
extern (C++) bool equals(ref const(Loc) loc) const
{
return (!showColumns || charnum == loc.charnum) &&
linnum == loc.linnum &&
FileName.equals(filename, loc.filename);
SourceLoc lhs = SourceLoc(this);
SourceLoc rhs = SourceLoc(loc);
return (!showColumns || lhs.column == rhs.column) &&
lhs.line == rhs.line &&
FileName.equals(lhs.filename, rhs.filename);
}
/**
@ -198,6 +208,8 @@ void writeSourceLoc(ref OutBuffer buf,
bool showColumns,
MessageStyle messageStyle) nothrow
{
if (loc.filename.length == 0)
return;
buf.writestring(loc.filename);
if (loc.line == 0)
return;
@ -258,4 +270,18 @@ struct SourceLoc
this.line = loc.linnum;
this.column = loc.charnum;
}
extern (C++) const(char)* toChars(
bool showColumns = Loc.showColumns,
MessageStyle messageStyle = Loc.messageStyle) const nothrow
{
OutBuffer buf;
writeSourceLoc(buf, this, showColumns, messageStyle);
return buf.extractChars();
}
bool opEquals(SourceLoc other) const nothrow
{
return this.filename == other.filename && this.line == other.line && this.column == other.column;
}
}

View file

@ -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());
}

View file

@ -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

View file

@ -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);
}

View file

@ -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,

View file

@ -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;
@ -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;
@ -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;

View file

@ -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);
@ -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);
@ -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);
@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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);
@ -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);

View file

@ -129,7 +129,7 @@ unittest
}
}.stripDelimited;
Loc loc = Loc(filename, 5, 20);
SourceLoc loc = SourceLoc(filename, 5, 20);
enum message = "Error: function test.Foo.foo only functions with Objective-C linkage can be declared as optional";
enum supplemental = "function is declared with D linkage";
auto expected = [Diagnostic(loc, message), Diagnostic(loc, supplemental)];
@ -154,7 +154,7 @@ unittest
}
}.stripDelimited;
Loc loc = Loc(filename, 6, 30);
SourceLoc loc = SourceLoc(filename, 6, 30);
enum message = "Error: function test.Foo.foo can only declare a function as optional once";
auto expected = Diagnostic(loc, message);
@ -184,15 +184,13 @@ unittest
}
}.stripDelimited;
Loc loc = Loc(filename, 6, 20);
auto expected = [
Diagnostic(
Loc(filename, 6, 20),
SourceLoc(filename, 6, 20),
"Error: function test.Foo.foo!().foo template cannot be optional"
),
Diagnostic(
Loc(filename, 12, 10),
SourceLoc(filename, 12, 10),
"Error: template instance test.Foo.foo!() error instantiating"
)
];
@ -223,7 +221,7 @@ unittest
}
}.stripDelimited;
Loc loc = Loc(filename, 6, 20);
SourceLoc loc = SourceLoc(filename, 6, 20);
enum message = "Error: function test.Foo.foo only functions declared inside interfaces can be optional";
enum supplemental = "function is declared inside class";
auto expected = [Diagnostic(loc, message), Diagnostic(loc, supplemental)];

View file

@ -29,7 +29,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;
@ -52,7 +52,7 @@ unittest
{
int supplementalCount;
override bool errorSupplemental(const ref Loc, const(char)*, va_list, const(char)*, const(char)*)
override bool errorSupplemental(const ref SourceLoc, const(char)*, va_list, const(char)*, const(char)*)
{
supplementalCount++;
return true;
@ -79,7 +79,7 @@ unittest
{
int warningCount;
override bool warning(const ref Loc, const(char)*, va_list, const(char)*, const(char)*)
override bool warning(const ref SourceLoc, const(char)*, va_list, const(char)*, const(char)*)
{
warningCount++;
return true;

View file

@ -115,19 +115,19 @@ class NoopDiagnosticReporter : DiagnosticReporter
override int errorCount() { return 0; }
override int warningCount() { return 0; }
override int deprecationCount() { return 0; }
override bool error(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool errorSupplemental(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool warning(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool warningSupplemental(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool deprecation(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool deprecationSupplemental(const ref Loc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool error(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool errorSupplemental(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool warning(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool warningSupplemental(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool deprecation(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
override bool deprecationSupplemental(const ref SourceLoc loc, const(char)* format, va_list, const(char)* p1, const(char)* p2) { return true; }
}
/// A single diagnostic.
const struct Diagnostic
{
/// The location of the diagnostic.
Loc location;
SourceLoc location;
/// The diagnostic message.
string message;
@ -157,7 +157,7 @@ struct DiagnosticCollector
/// Handles a diagnostic.
bool handleDiagnostic (
const ref Loc location,
const ref SourceLoc location,
Color headerColor,
const(char)* header,
const(char)* messageFormat,