[clang-tools-extra] r287758 - [clang-rename] Fix non-functional offset check.

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 23 05:10:08 PST 2016


Author: d0k
Date: Wed Nov 23 07:10:07 2016
New Revision: 287758

URL: http://llvm.org/viewvc/llvm-project?rev=287758&view=rev
Log:
[clang-rename] Fix non-functional offset check.

Adding something to a SourceLocation will only produce an invalid
SourceLocation in edge cases (overflow or adding 0 to an invalid one).
Check that the offset is inside the file instead and add a test case to
verify that the error message works.

Modified:
    clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp

Modified: clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp?rev=287758&r1=287757&r2=287758&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp (original)
+++ clang-tools-extra/trunk/clang-rename/USRFindingAction.cpp Wed Nov 23 07:10:07 2016
@@ -150,21 +150,20 @@ private:
   bool FindSymbol(ASTContext &Context, const SourceManager &SourceMgr,
                   unsigned SymbolOffset, const std::string &QualifiedName) {
     DiagnosticsEngine &Engine = Context.getDiagnostics();
+    const FileID MainFileID = SourceMgr.getMainFileID();
 
-    const SourceLocation Point =
-        SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID())
-            .getLocWithOffset(SymbolOffset);
-
-    if (!Point.isValid()) {
+    if (SymbolOffset >= SourceMgr.getFileIDSize(MainFileID)) {
       ErrorOccurred = true;
       unsigned InvalidOffset = Engine.getCustomDiagID(
           DiagnosticsEngine::Error,
           "SourceLocation in file %0 at offset %1 is invalid");
-      Engine.Report(Point, InvalidOffset) << SourceMgr.getFilename(Point)
-                                          << SymbolOffset;
+      Engine.Report(SourceLocation(), InvalidOffset)
+          << SourceMgr.getFileEntryForID(MainFileID)->getName() << SymbolOffset;
       return false;
     }
 
+    const SourceLocation Point = SourceMgr.getLocForStartOfFile(MainFileID)
+                                     .getLocWithOffset(SymbolOffset);
     const NamedDecl *FoundDecl = QualifiedName.empty()
                                      ? getNamedDeclAt(Context, Point)
                                      : getNamedDeclFor(Context, QualifiedName);




More information about the cfe-commits mailing list