clang  7.0.0svn
Go to the documentation of this file.
1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- 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 ///
10 /// \file
11 /// This file contains the declaration of the ODRHash class, which calculates
12 /// a hash based on AST nodes, which is stable across different runs.
13 ///
14 //===----------------------------------------------------------------------===//
17 #include "clang/AST/Type.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallVector.h"
24 namespace clang {
26 class Decl;
27 class IdentifierInfo;
28 class NestedNameSpecifier;
29 class Stmt;
30 class TemplateParameterList;
32 // ODRHash is used to calculate a hash based on AST node contents that
33 // does not rely on pointer addresses. This allows the hash to not vary
34 // between runs and is usable to detect ODR problems in modules. To use,
35 // construct an ODRHash object, then call Add* methods over the nodes that
36 // need to be hashed. Then call CalculateHash to get the hash value.
37 // Typically, only one Add* call is needed. clear can be called to reuse the
38 // object.
39 class ODRHash {
40  // Use DenseMaps to convert from DeclarationName and Type pointers
41  // to an index value.
42  llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
43  llvm::DenseMap<const Type*, unsigned> TypeMap;
45  // Save space by processing bools at the end.
48  llvm::FoldingSetNodeID ID;
50 public:
51  ODRHash() {}
53  // Use this for ODR checking classes between modules. This method compares
54  // more information than the AddDecl class.
55  void AddCXXRecordDecl(const CXXRecordDecl *Record);
57  // Use this for ODR checking functions between modules. This method compares
58  // more information than the AddDecl class.
59  void AddFunctionDecl(const FunctionDecl *Function);
61  // Process SubDecls of the main Decl. This method calls the DeclVisitor
62  // while AddDecl does not.
63  void AddSubDecl(const Decl *D);
65  // Reset the object for reuse.
66  void clear();
68  // Add booleans to ID and uses it to calculate the hash.
69  unsigned CalculateHash();
71  // Add AST nodes that need to be processed.
72  void AddDecl(const Decl *D);
73  void AddType(const Type *T);
74  void AddQualType(QualType T);
75  void AddStmt(const Stmt *S);
76  void AddIdentifierInfo(const IdentifierInfo *II);
78  void AddTemplateName(TemplateName Name);
83  // Save booleans until the end to lower the size of data to process.
84  void AddBoolean(bool value);
86  static bool isWhitelistedDecl(const Decl* D, const CXXRecordDecl *Record);
87 };
89 } // end namespace clang
Represents a function declaration or definition.
Definition: Decl.h:1714
void AddFunctionDecl(const FunctionDecl *Function)
Definition: ODRHash.cpp:509
A (possibly-)qualified type.
Definition: Type.h:655
void AddBoolean(bool value)
Definition: ODRHash.cpp:793
Stmt - This represents one statement.
Definition: Stmt.h:66
void AddQualType(QualType T)
Definition: ODRHash.cpp:784
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The base class of the type hierarchy.
Definition: Type.h:1421
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:68
void AddTemplateArgument(TemplateArgument TA)
Definition: ODRHash.cpp:140
void clear()
Definition: ODRHash.cpp:181
static bool isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Record)
Definition: ODRHash.cpp:438
One of these records is kept for each identifier that is lexed.
void AddTemplateParameterList(const TemplateParameterList *TPL)
Definition: ODRHash.cpp:172
void AddDecl(const Decl *D)
Definition: ODRHash.cpp:539
void AddTemplateName(TemplateName Name)
Definition: ODRHash.cpp:122
const FunctionProtoType * T
void AddDeclarationName(DeclarationName Name)
Definition: ODRHash.cpp:35
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
void AddType(const Type *T)
Definition: ODRHash.cpp:771
void AddIdentifierInfo(const IdentifierInfo *II)
Definition: ODRHash.cpp:30
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS)
Definition: ODRHash.cpp:93
void AddSubDecl(const Decl *D)
Definition: ODRHash.cpp:460
Represents a template argument.
Definition: TemplateBase.h:51
Dataflow Directional Tag Classes.
void AddCXXRecordDecl(const CXXRecordDecl *Record)
Definition: ODRHash.cpp:466
DeclarationName - The name of a declaration.
unsigned CalculateHash()
Definition: ODRHash.cpp:188
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
void AddStmt(const Stmt *S)
Definition: ODRHash.cpp:25