Browse Source

[analyzer] Fix IssueHash generation.

Differential Revision: http://reviews.llvm.org/D14919 

Original patch by: Gyorgy Orban!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@254394 91177308-0d34-0410-b5e6-96231b3b80d8
Gabor Horvath 9 years ago
parent
commit
d69b791aec

+ 4 - 2
include/clang/StaticAnalyzer/Core/IssueHash.h

@@ -15,6 +15,7 @@ namespace clang {
 class Decl;
 class SourceManager;
 class FullSourceLoc;
+class LangOptions;
 
 /// \brief Get an MD5 hash to help identify bugs.
 ///
@@ -37,13 +38,14 @@ class FullSourceLoc;
 llvm::SmallString<32> GetIssueHash(const SourceManager &SM,
                                    FullSourceLoc &IssueLoc,
                                    llvm::StringRef CheckerName,
-                                   llvm::StringRef BugType, const Decl *D);
+                                   llvm::StringRef BugType, const Decl *D,
+                                   const LangOptions &LangOpts);
 
 /// \brief Get the string representation of issue hash. See GetIssueHash() for
 /// more information.
 std::string GetIssueString(const SourceManager &SM, FullSourceLoc &IssueLoc,
                            llvm::StringRef CheckerName, llvm::StringRef BugType,
-                           const Decl *D);
+                           const Decl *D, const LangOptions &LangOpts);
 } // namespace clang
 
 #endif

+ 1 - 1
lib/Basic/SourceManager.cpp

@@ -1717,7 +1717,7 @@ SourceLocation SourceManager::translateLineCol(FileID FID,
                                                unsigned Col) const {
   // Lines are used as a one-based index into a zero-based array. This assert
   // checks for possible buffer underruns.
-  assert(Line != 0 && "Passed a zero-based line");
+  assert(Line && Col && "Line and column should start from 1!");
 
   if (FID.isInvalid())
     return SourceLocation();

+ 2 - 1
lib/StaticAnalyzer/Checkers/DebugCheckers.cpp

@@ -230,11 +230,12 @@ public:
     if (!N)
       return;
 
+    const LangOptions &Opts = C.getLangOpts();
     const SourceManager &SM = C.getSourceManager();
     FullSourceLoc FL(S->getLocStart(), SM);
     std::string HashContent =
         GetIssueString(SM, FL, getCheckName().getName(), BT->getCategory(),
-                       C.getLocationContext()->getDecl());
+                       C.getLocationContext()->getDecl(), Opts);
 
     C.emitReport(llvm::make_unique<BugReport>(*BT, HashContent, N));
   }

+ 2 - 2
lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp

@@ -255,8 +255,8 @@ void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
     os  << "\n<!-- FUNCTIONNAME " <<  declName << " -->\n";
 
     os << "\n<!-- ISSUEHASHCONTENTOFLINEINCONTEXT "
-       << GetIssueHash(SMgr, L, D.getCheckName(), D.getBugType(), DeclWithIssue)
-       << " -->\n";
+       << GetIssueHash(SMgr, L, D.getCheckName(), D.getBugType(), DeclWithIssue,
+                       PP.getLangOpts()) << " -->\n";
 
     os << "\n<!-- BUGLINE "
        << LineNumber

+ 10 - 8
lib/StaticAnalyzer/Core/IssueHash.cpp

@@ -127,14 +127,13 @@ static StringRef GetNthLineOfFile(llvm::MemoryBuffer *Buffer, int Line) {
 }
 
 static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
-                                 const Decl *D) {
+                                 const LangOptions &LangOpts) {
   static StringRef Whitespaces = " \t\n";
 
-  const LangOptions &Opts = D->getASTContext().getLangOpts();
   StringRef Str = GetNthLineOfFile(SM.getBuffer(L.getFileID(), L),
                                    L.getExpansionLineNumber());
   unsigned col = Str.find_first_not_of(Whitespaces);
-
+  col++;
   SourceLocation StartOfLine =
       SM.translateLineCol(SM.getFileID(L), L.getExpansionLineNumber(), col);
   llvm::MemoryBuffer *Buffer =
@@ -145,7 +144,7 @@ static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
   const char *BufferPos = SM.getCharacterData(StartOfLine);
 
   Token Token;
-  Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), Opts,
+  Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), LangOpts,
               Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd());
 
   size_t NextStart = 0;
@@ -175,20 +174,23 @@ static llvm::SmallString<32> GetHashOfContent(StringRef Content) {
 std::string clang::GetIssueString(const SourceManager &SM,
                                   FullSourceLoc &IssueLoc,
                                   StringRef CheckerName, StringRef BugType,
-                                  const Decl *D) {
+                                  const Decl *D,
+                                  const LangOptions &LangOpts) {
   static StringRef Delimiter = "$";
 
   return (llvm::Twine(CheckerName) + Delimiter +
           GetEnclosingDeclContextSignature(D) + Delimiter +
           llvm::utostr(IssueLoc.getExpansionColumnNumber()) + Delimiter +
-          NormalizeLine(SM, IssueLoc, D) + Delimiter + BugType)
+          NormalizeLine(SM, IssueLoc, LangOpts) + Delimiter + BugType)
       .str();
 }
 
 SmallString<32> clang::GetIssueHash(const SourceManager &SM,
                                     FullSourceLoc &IssueLoc,
                                     StringRef CheckerName, StringRef BugType,
-                                    const Decl *D) {
+                                    const Decl *D,
+                                    const LangOptions &LangOpts) {
+
   return GetHashOfContent(
-      GetIssueString(SM, IssueLoc, CheckerName, BugType, D));
+      GetIssueString(SM, IssueLoc, CheckerName, BugType, D, LangOpts));
 }

+ 1 - 1
lib/StaticAnalyzer/Core/PlistDiagnostics.cpp

@@ -399,7 +399,7 @@ void PlistDiagnostics::FlushDiagnosticsImpl(
                     *SM);
     const Decl *DeclWithIssue = D->getDeclWithIssue();
     EmitString(o, GetIssueHash(*SM, L, D->getCheckName(), D->getBugType(),
-                               DeclWithIssue))
+                               DeclWithIssue, LangOpts))
         << '\n';
 
     // Output information about the semantic context where

+ 4 - 4
test/Analysis/bug_hash_test.cpp

@@ -288,17 +288,17 @@ void testLambda() {
 // CHECK-NEXT:     </array>
 // CHECK-NEXT:     <key>depth</key><integer>0</integer>
 // CHECK-NEXT:     <key>extended_message</key>
-// CHECK-NEXT:     <string>debug.DumpBugHash$int f()$28$namespaceAA{$debug</string>
+// CHECK-NEXT:     <string>debug.DumpBugHash$int f()$28$constexprintf(){return5;}$debug</string>
 // CHECK-NEXT:     <key>message</key>
-// CHECK-NEXT:     <string>debug.DumpBugHash$int f()$28$namespaceAA{$debug</string>
+// CHECK-NEXT:     <string>debug.DumpBugHash$int f()$28$constexprintf(){return5;}$debug</string>
 // CHECK-NEXT:    </dict>
 // CHECK-NEXT:   </array>
-// CHECK-NEXT:   <key>description</key><string>debug.DumpBugHash$int f()$28$namespaceAA{$debug</string>
+// CHECK-NEXT:   <key>description</key><string>debug.DumpBugHash$int f()$28$constexprintf(){return5;}$debug</string>
 // CHECK-NEXT:   <key>category</key><string>debug</string>
 // CHECK-NEXT:   <key>type</key><string>Dump hash components</string>
 // CHECK-NEXT:   <key>check_name</key><string>debug.DumpBugHash</string>
 // CHECK-NEXT:   <!-- This hash is experimental and going to change! -->
-// CHECK-NEXT:   <key>issue_hash_content_of_line_in_context</key><string>f8ee38da3de42e209c4afa886b5531ab</string>
+// CHECK-NEXT:   <key>issue_hash_content_of_line_in_context</key><string>f5471f52854dc14167fe96db50c4ba5f</string>
 // CHECK-NEXT:  <key>issue_context_kind</key><string>function</string>
 // CHECK-NEXT:  <key>issue_context</key><string>f</string>
 // CHECK-NEXT:  <key>issue_hash_function_offset</key><string>0</string>

+ 1 - 1
test/Analysis/diagnostics/report-issues-within-main-file.cpp

@@ -949,7 +949,7 @@ void callInMacroArg() {
 // CHECK-NEXT:   <key>type</key><string>Bad deallocator</string>
 // CHECK-NEXT:   <key>check_name</key><string>unix.MismatchedDeallocator</string>
 // CHECK-NEXT:   <!-- This hash is experimental and going to change! -->
-// CHECK-NEXT:   <key>issue_hash_content_of_line_in_context</key><string>f21ac032efaa3d1459a5ed31f0ad44f0</string>
+// CHECK-NEXT:   <key>issue_hash_content_of_line_in_context</key><string>f689fbd54138491e228f0f89bb02bfb2</string>
 // CHECK-NEXT:  <key>issue_context_kind</key><string>function</string>
 // CHECK-NEXT:  <key>issue_context</key><string>mainPlusHeader</string>
 // CHECK-NEXT:  <key>issue_hash_function_offset</key><string>2</string>