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/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/LineIterator.h"
21 #include "llvm/Support/MD5.h"
22 #include "llvm/Support/Path.h"
28 using namespace clang;
30 // Get a string representation of the parts of the signature that can be
32 static std::string GetSignature(const FunctionDecl *Target) {
35 std::string Signature;
37 if (!isa<CXXConstructorDecl>(Target) && !isa<CXXDestructorDecl>(Target) &&
38 !isa<CXXConversionDecl>(Target))
39 Signature.append(Target->getReturnType().getAsString()).append(" ");
40 Signature.append(Target->getQualifiedNameAsString()).append("(");
42 for (int i = 0, paramsCount = Target->getNumParams(); i < paramsCount; ++i) {
44 Signature.append(", ");
45 Signature.append(Target->getParamDecl(i)->getType().getAsString());
48 if (Target->isVariadic())
49 Signature.append(", ...");
50 Signature.append(")");
53 llvm::dyn_cast_or_null<FunctionType>(Target->getType().getTypePtr());
55 if (!TargetT || !isa<CXXMethodDecl>(Target))
58 if (TargetT->isConst())
59 Signature.append(" const");
60 if (TargetT->isVolatile())
61 Signature.append(" volatile");
62 if (TargetT->isRestrict())
63 Signature.append(" restrict");
65 if (const auto *TargetPT =
66 dyn_cast_or_null<FunctionProtoType>(Target->getType().getTypePtr())) {
67 switch (TargetPT->getRefQualifier()) {
69 Signature.append(" &");
72 Signature.append(" &&");
82 static std::string GetEnclosingDeclContextSignature(const Decl *D) {
86 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
89 switch (ND->getKind()) {
94 DeclName = ND->getQualifiedNameAsString();
96 case Decl::CXXConstructor:
97 case Decl::CXXDestructor:
98 case Decl::CXXConversion:
101 DeclName = GetSignature(dyn_cast_or_null<FunctionDecl>(ND));
103 case Decl::ObjCMethod:
104 // ObjC Methods can not be overloaded, qualified name uniquely identifies
106 DeclName = ND->getQualifiedNameAsString();
118 static StringRef GetNthLineOfFile(llvm::MemoryBuffer *Buffer, int Line) {
122 llvm::line_iterator LI(*Buffer, false);
123 for (; !LI.is_at_eof() && LI.line_number() != Line; ++LI)
129 static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
130 const LangOptions &LangOpts) {
131 static StringRef Whitespaces = " \t\n";
133 StringRef Str = GetNthLineOfFile(SM.getBuffer(L.getFileID(), L),
134 L.getExpansionLineNumber());
135 StringRef::size_type col = Str.find_first_not_of(Whitespaces);
136 if (col == StringRef::npos)
137 col = 1; // The line only contains whitespace.
140 SourceLocation StartOfLine =
141 SM.translateLineCol(SM.getFileID(L), L.getExpansionLineNumber(), col);
142 llvm::MemoryBuffer *Buffer =
143 SM.getBuffer(SM.getFileID(StartOfLine), StartOfLine);
147 const char *BufferPos = SM.getCharacterData(StartOfLine);
150 Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), LangOpts,
151 Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd());
153 size_t NextStart = 0;
154 std::ostringstream LineBuff;
155 while (!Lexer.LexFromRawLexer(Token) && NextStart < 2) {
156 if (Token.isAtStartOfLine() && NextStart++ > 0)
158 LineBuff << std::string(SM.getCharacterData(Token.getLocation()),
162 return LineBuff.str();
165 static llvm::SmallString<32> GetHashOfContent(StringRef Content) {
167 llvm::MD5::MD5Result MD5Res;
170 Hash.update(Content);
172 llvm::MD5::stringifyResult(MD5Res, Res);
177 std::string clang::GetIssueString(const SourceManager &SM,
178 FullSourceLoc &IssueLoc,
179 StringRef CheckerName, StringRef BugType,
181 const LangOptions &LangOpts) {
182 static StringRef Delimiter = "$";
184 return (llvm::Twine(CheckerName) + Delimiter +
185 GetEnclosingDeclContextSignature(D) + Delimiter +
186 Twine(IssueLoc.getExpansionColumnNumber()) + Delimiter +
187 NormalizeLine(SM, IssueLoc, LangOpts) + Delimiter + BugType)
191 SmallString<32> clang::GetIssueHash(const SourceManager &SM,
192 FullSourceLoc &IssueLoc,
193 StringRef CheckerName, StringRef BugType,
195 const LangOptions &LangOpts) {
197 return GetHashOfContent(
198 GetIssueString(SM, IssueLoc, CheckerName, BugType, D, LangOpts));