|
| 1 | +//===---------- IssueHash.cpp - Generate identification hashes --*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 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/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 <functional> |
| 24 | +#include <sstream> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +using namespace clang; |
| 28 | + |
| 29 | +// Get a string representation of the parts of the signature that can be |
| 30 | +// overloaded on. |
| 31 | +static std::string GetSignature(const FunctionDecl *Target) { |
| 32 | + if (!Target) |
| 33 | + return ""; |
| 34 | + std::string Signature; |
| 35 | + |
| 36 | + if (!isa<CXXConstructorDecl>(Target) && !isa<CXXDestructorDecl>(Target) && |
| 37 | + !isa<CXXConversionDecl>(Target)) |
| 38 | + Signature.append(Target->getReturnType().getAsString()).append(" "); |
| 39 | + Signature.append(Target->getQualifiedNameAsString()).append("("); |
| 40 | + |
| 41 | + for (int i = 0, paramsCount = Target->getNumParams(); i < paramsCount; ++i) { |
| 42 | + if (i) |
| 43 | + Signature.append(", "); |
| 44 | + Signature.append(Target->getParamDecl(i)->getType().getAsString()); |
| 45 | + } |
| 46 | + |
| 47 | + if (Target->isVariadic()) |
| 48 | + Signature.append(", ..."); |
| 49 | + Signature.append(")"); |
| 50 | + |
| 51 | + const auto *TargetT = |
| 52 | + llvm::dyn_cast_or_null<FunctionType>(Target->getType().getTypePtr()); |
| 53 | + |
| 54 | + if (!TargetT) |
| 55 | + return Signature; |
| 56 | + |
| 57 | + if (TargetT->isConst()) |
| 58 | + Signature.append(" const"); |
| 59 | + if (TargetT->isVolatile()) |
| 60 | + Signature.append(" volatile"); |
| 61 | + if (TargetT->isRestrict()) |
| 62 | + Signature.append(" restrict"); |
| 63 | + |
| 64 | + if (const auto *TargetPT = |
| 65 | + dyn_cast_or_null<FunctionProtoType>(Target->getType().getTypePtr())) { |
| 66 | + switch (TargetPT->getRefQualifier()) { |
| 67 | + case RQ_LValue: |
| 68 | + Signature.append(" &"); |
| 69 | + break; |
| 70 | + case RQ_RValue: |
| 71 | + Signature.append(" &&"); |
| 72 | + break; |
| 73 | + default: |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return Signature; |
| 79 | +} |
| 80 | + |
| 81 | +static std::string GetEnclosingDeclContextSignature(const Decl *D) { |
| 82 | + if (!D) |
| 83 | + return ""; |
| 84 | + |
| 85 | + if (const auto *ND = dyn_cast<NamedDecl>(D)) { |
| 86 | + std::string DeclName; |
| 87 | + |
| 88 | + switch (ND->getKind()) { |
| 89 | + case Decl::Namespace: |
| 90 | + case Decl::Record: |
| 91 | + case Decl::CXXRecord: |
| 92 | + case Decl::Enum: |
| 93 | + DeclName = ND->getQualifiedNameAsString(); |
| 94 | + break; |
| 95 | + case Decl::CXXConstructor: |
| 96 | + case Decl::CXXDestructor: |
| 97 | + case Decl::CXXConversion: |
| 98 | + case Decl::CXXMethod: |
| 99 | + case Decl::Function: |
| 100 | + DeclName = GetSignature(dyn_cast_or_null<FunctionDecl>(ND)); |
| 101 | + break; |
| 102 | + case Decl::ObjCMethod: |
| 103 | + // ObjC Methods can not be overloaded, qualified name uniquely identifies |
| 104 | + // the method. |
| 105 | + DeclName = ND->getQualifiedNameAsString(); |
| 106 | + break; |
| 107 | + default: |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + return DeclName; |
| 112 | + } |
| 113 | + |
| 114 | + return ""; |
| 115 | +} |
| 116 | + |
| 117 | +static StringRef GetNthLineOfFile(llvm::MemoryBuffer *Buffer, int Line) { |
| 118 | + if (!Buffer) |
| 119 | + return ""; |
| 120 | + |
| 121 | + llvm::line_iterator LI(*Buffer, false); |
| 122 | + for (; !LI.is_at_eof() && LI.line_number() != Line; ++LI) |
| 123 | + ; |
| 124 | + |
| 125 | + return *LI; |
| 126 | +} |
| 127 | + |
| 128 | +static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L, |
| 129 | + const Decl *D) { |
| 130 | + static StringRef Whitespaces = " \t\n"; |
| 131 | + |
| 132 | + const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 133 | + StringRef Str = GetNthLineOfFile(SM.getBuffer(L.getFileID(), L), |
| 134 | + L.getExpansionLineNumber()); |
| 135 | + unsigned col = Str.find_first_not_of(Whitespaces); |
| 136 | + |
| 137 | + SourceLocation StartOfLine = |
| 138 | + SM.translateLineCol(SM.getFileID(L), L.getExpansionLineNumber(), col); |
| 139 | + llvm::MemoryBuffer *Buffer = |
| 140 | + SM.getBuffer(SM.getFileID(StartOfLine), StartOfLine); |
| 141 | + if (!Buffer) |
| 142 | + return {}; |
| 143 | + |
| 144 | + const char *BufferPos = SM.getCharacterData(StartOfLine); |
| 145 | + |
| 146 | + Token Token; |
| 147 | + Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(StartOfLine)), Opts, |
| 148 | + Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd()); |
| 149 | + |
| 150 | + size_t NextStart = 0; |
| 151 | + std::ostringstream LineBuff; |
| 152 | + while (!Lexer.LexFromRawLexer(Token) && NextStart < 2) { |
| 153 | + if (Token.isAtStartOfLine() && NextStart++ > 0) |
| 154 | + continue; |
| 155 | + LineBuff << std::string(SM.getCharacterData(Token.getLocation()), |
| 156 | + Token.getLength()); |
| 157 | + } |
| 158 | + |
| 159 | + return LineBuff.str(); |
| 160 | +} |
| 161 | + |
| 162 | +static llvm::SmallString<32> GetHashOfContent(StringRef Content) { |
| 163 | + llvm::MD5 Hash; |
| 164 | + llvm::MD5::MD5Result MD5Res; |
| 165 | + SmallString<32> Res; |
| 166 | + |
| 167 | + Hash.update(Content); |
| 168 | + Hash.final(MD5Res); |
| 169 | + llvm::MD5::stringifyResult(MD5Res, Res); |
| 170 | + |
| 171 | + return Res; |
| 172 | +} |
| 173 | + |
| 174 | +std::string clang::GetIssueString(const SourceManager &SM, |
| 175 | + FullSourceLoc &IssueLoc, |
| 176 | + StringRef CheckerName, StringRef BugType, |
| 177 | + const Decl *D) { |
| 178 | + static StringRef Delimiter = "$"; |
| 179 | + |
| 180 | + return (llvm::Twine(CheckerName) + Delimiter + |
| 181 | + GetEnclosingDeclContextSignature(D) + Delimiter + |
| 182 | + std::to_string(IssueLoc.getExpansionColumnNumber()) + Delimiter + |
| 183 | + NormalizeLine(SM, IssueLoc, D) + Delimiter + BugType) |
| 184 | + .str(); |
| 185 | +} |
| 186 | + |
| 187 | +SmallString<32> clang::GetIssueHash(const SourceManager &SM, |
| 188 | + FullSourceLoc &IssueLoc, |
| 189 | + StringRef CheckerName, StringRef BugType, |
| 190 | + const Decl *D) { |
| 191 | + return GetHashOfContent( |
| 192 | + GetIssueString(SM, IssueLoc, CheckerName, BugType, D)); |
| 193 | +} |
0 commit comments