]> granicus.if.org Git - clang/commitdiff
[analyzer] Fix IssueHash generation.
authorGabor Horvath <xazax.hun@gmail.com>
Tue, 1 Dec 2015 09:00:41 +0000 (09:00 +0000)
committerGabor Horvath <xazax.hun@gmail.com>
Tue, 1 Dec 2015 09:00:41 +0000 (09:00 +0000)
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

include/clang/StaticAnalyzer/Core/IssueHash.h
lib/Basic/SourceManager.cpp
lib/StaticAnalyzer/Checkers/DebugCheckers.cpp
lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
lib/StaticAnalyzer/Core/IssueHash.cpp
lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
test/Analysis/bug_hash_test.cpp
test/Analysis/diagnostics/report-issues-within-main-file.cpp

index 913ab7964fb80dfe47a52b4e635ad1ad17a251ed..b3c4f1465594e1098da95910fac69ce612272045 100644 (file)
@@ -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
index 80a003fc932fa14556dff96114c4140bb189297b..4c501616a3e8fffe2cdc56e7c493e89f2cf66df6 100644 (file)
@@ -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();
index acf0004136588f88b122c2cd335d320f6339dcf5..2eef1688d4c4b22c9d1df5a0f9e23017cfbd4d6f 100644 (file)
@@ -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));
   }
index 86eab41a4ccb2fbe6bc860c107824398ca92d0fa..183acbe1d91e4c6c134540bd2c5287cfa11b5b7f 100644 (file)
@@ -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
index abe20d6774f8dfb25baeb74b8a04937fc668c0d7..0a3af3dcc7e94e2223e4022eb8e0440ab681aac4 100644 (file)
@@ -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));
 }
index 2bf439477b031db0a3c6667a1b2a2cc3e25cd570..3e428fa9224ccaec4b1f89a622d18eff0d299acb 100644 (file)
@@ -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
index 4b46004dcda150553a068a899ee6cc106adf3f44..b73528e88d2ffa6ae15d5fc1587d64d05e55dcad 100644 (file)
@@ -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>
index cb998965f76294f01016fa2cbb3937a95b311a55..5fd7abd03c4a2e4dc280a250c2f8ed52ef4e3f99 100644 (file)
@@ -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>