diff --git a/clang/include/clang/Basic/SourceLocation.h b/clang/include/clang/Basic/SourceLocation.h --- a/clang/include/clang/Basic/SourceLocation.h +++ b/clang/include/clang/Basic/SourceLocation.h @@ -132,9 +132,18 @@ } public: - /// Return a source location with the specified offset from this + /// Return a source location with a specified offset from this /// SourceLocation. - SourceLocation getLocWithOffset(IntType Offset) const { + /// + /// If the second parameter NegOffset is provided, then the combined offset + /// is computed as Offset - NegOffset. This is convenient if the desired + /// offset is held in a 32-bit integer type such as `unsigned`, but + /// SourceLocations are 64 bits: if you compute a negative offset in an + /// unsigned without widening it first, it becomes positive again. Allowing + /// the subtraction to be done inside this function avoids a cumbersome cast + /// at every call site. + SourceLocation getLocWithOffset(IntType Offset, IntType NegOffset = 0) const { + Offset -= NegOffset; assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow"); SourceLocation L; L.ID = ID+Offset; diff --git a/clang/lib/AST/SelectorLocationsKind.cpp b/clang/lib/AST/SelectorLocationsKind.cpp --- a/clang/lib/AST/SelectorLocationsKind.cpp +++ b/clang/lib/AST/SelectorLocationsKind.cpp @@ -28,7 +28,7 @@ return SourceLocation(); IdentifierInfo *II = Sel.getIdentifierInfoForSlot(0); unsigned Len = II ? II->getLength() : 0; - return EndLoc.getLocWithOffset(-Len); + return EndLoc.getLocWithOffset(0, Len); } assert(Index < NumSelArgs); @@ -38,7 +38,7 @@ unsigned Len = /* selector id */ (II ? II->getLength() : 0) + /* ':' */ 1; if (WithArgSpace) ++Len; - return ArgLoc.getLocWithOffset(-Len); + return ArgLoc.getLocWithOffset(0, Len); } namespace { diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -233,7 +233,7 @@ /// Return the start location of an included file or expanded macro. SourceLocation getStartOfFileOrMacro(SourceLocation Loc) { if (Loc.isMacroID()) - return Loc.getLocWithOffset(-SM.getFileOffset(Loc)); + return Loc.getLocWithOffset(0, SM.getFileOffset(Loc)); return SM.getLocForStartOfFile(SM.getFileID(Loc)); } diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -832,7 +832,7 @@ FormatTok = new (Allocator.Allocate()) FormatToken; readRawToken(*FormatTok); SourceLocation WhitespaceStart = - FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); + FormatTok->Tok.getLocation().getLocWithOffset(0, TrailingWhitespace); FormatTok->IsFirst = IsFirstToken; IsFirstToken = false; diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -527,7 +527,7 @@ return Loc; // Create a lexer starting at the beginning of this token. - SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second); + SourceLocation LexerStartLoc = Loc.getLocWithOffset(0, LocInfo.second); Lexer TheLexer(LexerStartLoc, LangOpts, Buffer.data(), LexStart, Buffer.end()); TheLexer.SetCommentRetentionState(true); @@ -570,7 +570,7 @@ SM.getDecomposedLoc(BeginFileLoc); assert(FileLocInfo.first == BeginFileLocInfo.first && FileLocInfo.second >= BeginFileLocInfo.second); - return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second); + return Loc.getLocWithOffset(BeginFileLocInfo.second, FileLocInfo.second); } namespace { diff --git a/clang/lib/Parse/ParseStmtAsm.cpp b/clang/lib/Parse/ParseStmtAsm.cpp --- a/clang/lib/Parse/ParseStmtAsm.cpp +++ b/clang/lib/Parse/ParseStmtAsm.cpp @@ -185,7 +185,7 @@ if (TokIndex < AsmToks.size()) { const Token &Tok = AsmToks[TokIndex]; Loc = Tok.getLocation(); - Loc = Loc.getLocWithOffset(Offset - TokOffset); + Loc = Loc.getLocWithOffset(Offset, TokOffset); } return Loc; } diff --git a/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp b/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp --- a/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp +++ b/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp @@ -1081,8 +1081,7 @@ // Highlight the range. Make the span tag the outermost tag for the // selected range. - SourceLocation E = - InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo); + SourceLocation E = InstantiationEnd.getLocWithOffset(EndColNo, OldEndColNo); html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); }