clang: lib/Analysis/IssueHash.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
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"
22
23#include
24#include
25#include
26#include
27
28using namespace clang;
29
30
31
34 return "";
35 std::string Signature;
36
37
38
39
42 Target = InstantiatedFrom;
43
44 if (!isa(Target) && !isa(Target) &&
45 !isa(Target))
46 Signature.append(Target->getReturnType().getAsString()).append(" ");
47 Signature.append(Target->getQualifiedNameAsString()).append("(");
48
49 for (int i = 0, paramsCount = Target->getNumParams(); i < paramsCount; ++i) {
50 if (i)
51 Signature.append(", ");
52 Signature.append(Target->getParamDecl(i)->getType().getAsString());
53 }
54
55 if (Target->isVariadic())
56 Signature.append(", ...");
57 Signature.append(")");
58
59 const auto *TargetT =
60 llvm::dyn_cast_or_null(Target->getType().getTypePtr());
61
62 if (!TargetT || !isa(Target))
63 return Signature;
64
65 if (TargetT->isConst())
66 Signature.append(" const");
67 if (TargetT->isVolatile())
68 Signature.append(" volatile");
69 if (TargetT->isRestrict())
70 Signature.append(" restrict");
71
72 if (const auto *TargetPT =
73 dyn_cast_or_null(Target->getType().getTypePtr())) {
74 switch (TargetPT->getRefQualifier()) {
76 Signature.append(" &");
77 break;
79 Signature.append(" &&");
80 break;
81 default:
82 break;
83 }
84 }
85
86 return Signature;
87}
88
90 if ()
91 return "";
92
93 if (const auto *ND = dyn_cast(D)) {
94 std::string DeclName;
95
96 switch (ND->getKind()) {
97 case Decl::Namespace:
98 case Decl::Record:
99 case Decl::CXXRecord:
100 case Decl::Enum:
101 DeclName = ND->getQualifiedNameAsString();
102 break;
103 case Decl::CXXConstructor:
104 case Decl::CXXDestructor:
105 case Decl::CXXConversion:
106 case Decl::CXXMethod:
107 case Decl::Function:
108 DeclName = GetSignature(dyn_cast_or_null(ND));
109 break;
110 case Decl::ObjCMethod:
111
112
113 DeclName = ND->getQualifiedNameAsString();
114 break;
115 default:
116 break;
117 }
118
119 return DeclName;
120 }
121
122 return "";
123}
124
125static StringRef GetNthLineOfFile(std::optionalllvm::MemoryBufferRef Buffer,
126 int Line) {
127 if (!Buffer)
128 return "";
129
130 llvm::line_iterator LI(*Buffer, false);
131 for (; !LI.is_at_eof() && LI.line_number() != Line; ++LI)
132 ;
133
134 return *LI;
135}
136
139 static StringRef Whitespaces = " \t\n";
140
143 StringRef::size_type col = Str.find_first_not_of(Whitespaces);
144 if (col == StringRef::npos)
145 col = 1;
146 else
147 col++;
150 std::optionalllvm::MemoryBufferRef Buffer =
151 SM.getBufferOrNone(SM.getFileID(StartOfLine), StartOfLine);
152 if (!Buffer)
153 return {};
154
155 const char *BufferPos = SM.getCharacterData(StartOfLine);
156
158 Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), LangOpts,
159 Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd());
160
161 size_t NextStart = 0;
162 std::ostringstream LineBuff;
165 continue;
168 }
169
170 return LineBuff.str();
171}
172
174 llvm::MD5 Hash;
175 llvm::MD5::MD5Result MD5Res;
177
178 Hash.update(Content);
179 Hash.final(MD5Res);
180 llvm::MD5::stringifyResult(MD5Res, Res);
181
182 return Res;
183}
184
186 StringRef CheckerName,
187 StringRef WarningMessage,
188 const Decl *IssueDecl,
190 static StringRef Delimiter = "$";
191
192 return (llvm::Twine(CheckerName) + Delimiter +
196 Delimiter + WarningMessage)
197 .str();
198}
199
201 StringRef CheckerName,
202 StringRef WarningMessage,
203 const Decl *IssueDecl,
205
207 IssueLoc, CheckerName, WarningMessage, IssueDecl, LangOpts));
208}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static std::string NormalizeLine(const SourceManager &SM, const FullSourceLoc &L, const LangOptions &LangOpts)
static llvm::SmallString< 32 > GetMD5HashOfContent(StringRef Content)
static std::string GetSignature(const FunctionDecl *Target)
static StringRef GetNthLineOfFile(std::optional< llvm::MemoryBufferRef > Buffer, int Line)
static std::string GetEnclosingDeclContextSignature(const Decl *D)
llvm::MachO::Target Target
Defines the SourceManager interface.
Defines various enumerations that describe declaration and type specifiers.
Decl - This represents one declaration (or definition), e.g.
A SourceLocation and its associated SourceManager.
unsigned getExpansionColumnNumber(bool *Invalid=nullptr) const
const SourceManager & getManager() const
unsigned getExpansionLineNumber(bool *Invalid=nullptr) const
Represents a function declaration or definition.
FunctionDecl * getTemplateInstantiationPattern(bool ForDefinition=true) const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
The JSON file list parser is used to communicate input to InstallAPI.
@ RQ_LValue
An lvalue ref-qualifier was provided (&).
@ RQ_RValue
An rvalue ref-qualifier was provided (&&).
llvm::SmallString< 32 > getIssueHash(const FullSourceLoc &IssueLoc, llvm::StringRef CheckerName, llvm::StringRef WarningMessage, const Decl *IssueDecl, const LangOptions &LangOpts)
Returns an opaque identifier for a diagnostic.
std::string getIssueString(const FullSourceLoc &IssueLoc, llvm::StringRef CheckerName, llvm::StringRef WarningMessage, const Decl *IssueDecl, const LangOptions &LangOpts)
Get the unhashed string representation of the V1 issue hash.