mirror of
https://github.com/dlang/dmd.git
synced 2025-04-27 05:30:13 +03:00
Refactor use of Loc/SourceLoc (#20785)
This commit is contained in:
parent
b2c9da1337
commit
b289b6a6dc
19 changed files with 101 additions and 80 deletions
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue