1 //===---------- IssueHash.cpp - Generate identification hashes --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/StaticAnalyzer/Core/IssueHash.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Basic/Specifiers.h"
15 #include "clang/Lex/Lexer.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/LineIterator.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/Path.h"
27 using namespace clang;
29 // Get a string representation of the parts of the signature that can be
31 static std::string GetSignature(const FunctionDecl *Target) {
34 std::string Signature;
36 // When a flow sensitive bug happens in templated code we should not generate
37 // distinct hash value for every instantiation. Use the signature from the
39 if (const FunctionDecl *InstantiatedFrom =
40 Target->getTemplateInstantiationPattern())
41 Target = InstantiatedFrom;
43 if (!isa<CXXConstructorDecl>(Target) && !isa<CXXDestructorDecl>(Target) &&
44 !isa<CXXConversionDecl>(Target))
45 Signature.append(Target->getReturnType().getAsString()).append(" ");
46 Signature.append(Target->getQualifiedNameAsString()).append("(");
48 for (int i = 0, paramsCount = Target->getNumParams(); i < paramsCount; ++i) {
50 Signature.append(", ");
51 Signature.append(Target->getParamDecl(i)->getType().getAsString());
54 if (Target->isVariadic())
55 Signature.append(", ...");
56 Signature.append(")");
59 llvm::dyn_cast_or_null<FunctionType>(Target->getType().getTypePtr());
61 if (!TargetT || !isa<CXXMethodDecl>(Target))
64 if (TargetT->isConst())
65 Signature.append(" const");
66 if (TargetT->isVolatile())
67 Signature.append(" volatile");
68 if (TargetT->isRestrict())
69 Signature.append(" restrict");
71 if (const auto *TargetPT =
72 dyn_cast_or_null<FunctionProtoType>(Target->getType().getTypePtr())) {
73 switch (TargetPT->getRefQualifier()) {
75 Signature.append(" &");
78 Signature.append(" &&");
88 static std::string GetEnclosingDeclContextSignature(const Decl *D) {
92 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
95 switch (ND->getKind()) {
100 DeclName = ND->getQualifiedNameAsString();
102 case Decl::CXXConstructor:
103 case Decl::CXXDestructor:
104 case Decl::CXXConversion:
105 case Decl::CXXMethod:
107 DeclName = GetSignature(dyn_cast_or_null<FunctionDecl>(ND));
109 case Decl::ObjCMethod:
110 // ObjC Methods can not be overloaded, qualified name uniquely identifies
112 DeclName = ND->getQualifiedNameAsString();
124 static StringRef GetNthLineOfFile(llvm::MemoryBuffer *Buffer, int Line) {
128 llvm::line_iterator LI(*Buffer, false);
129 for (; !LI.is_at_eof() && LI.line_number() != Line; ++LI)
135 static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
136 const LangOptions &LangOpts) {
137 static StringRef Whitespaces = " \t\n";
139 StringRef Str = GetNthLineOfFile(SM.getBuffer(L.getFileID(), L),
140 L.getExpansionLineNumber());
141 StringRef::size_type col = Str.find_first_not_of(Whitespaces);
142 if (col == StringRef::npos)
143 col = 1; // The line only contains whitespace.
146 SourceLocation StartOfLine =
147 SM.translateLineCol(SM.getFileID(L), L.getExpansionLineNumber(), col);
148 llvm::MemoryBuffer *Buffer =
149 SM.getBuffer(SM.getFileID(StartOfLine), StartOfLine);
153 const char *BufferPos = SM.getCharacterData(StartOfLine);
156 Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), LangOpts,
157 Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd());
159 size_t NextStart = 0;
160 std::ostringstream LineBuff;
161 while (!Lexer.LexFromRawLexer(Token) && NextStart < 2) {
162 if (Token.isAtStartOfLine() && NextStart++ > 0)
164 LineBuff << std::string(SM.getCharacterData(Token.getLocation()),
168 return LineBuff.str();
171 static llvm::SmallString<32> GetHashOfContent(StringRef Content) {
173 llvm::MD5::MD5Result MD5Res;
176 Hash.update(Content);
178 llvm::MD5::stringifyResult(MD5Res, Res);
183 std::string clang::GetIssueString(const SourceManager &SM,
184 FullSourceLoc &IssueLoc,
185 StringRef CheckerName, StringRef BugType,
187 const LangOptions &LangOpts) {
188 static StringRef Delimiter = "$";
190 return (llvm::Twine(CheckerName) + Delimiter +
191 GetEnclosingDeclContextSignature(D) + Delimiter +
192 Twine(IssueLoc.getExpansionColumnNumber()) + Delimiter +
193 NormalizeLine(SM, IssueLoc, LangOpts) + Delimiter + BugType)
197 SmallString<32> clang::GetIssueHash(const SourceManager &SM,
198 FullSourceLoc &IssueLoc,
199 StringRef CheckerName, StringRef BugType,
201 const LangOptions &LangOpts) {
203 return GetHashOfContent(
204 GetIssueString(SM, IssueLoc, CheckerName, BugType, D, LangOpts));