clang  8.0.0svn
ODRHash.h
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 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_AST_ODRHASH_H
17 #define LLVM_CLANG_AST_ODRHASH_H
18 
20 #include "clang/AST/Type.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/PointerUnion.h"
25 #include "llvm/ADT/SmallVector.h"
26 
27 namespace clang {
28 
29 class Decl;
30 class IdentifierInfo;
31 class NestedNameSpecifier;
32 class Stmt;
33 class TemplateParameterList;
34 
35 // ODRHash is used to calculate a hash based on AST node contents that
36 // does not rely on pointer addresses. This allows the hash to not vary
37 // between runs and is usable to detect ODR problems in modules. To use,
38 // construct an ODRHash object, then call Add* methods over the nodes that
39 // need to be hashed. Then call CalculateHash to get the hash value.
40 // Typically, only one Add* call is needed. clear can be called to reuse the
41 // object.
42 class ODRHash {
43  // Use DenseMaps to convert from DeclarationName and Type pointers
44  // to an index value.
45  llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
46 
47  // Save space by processing bools at the end.
49 
50  llvm::FoldingSetNodeID ID;
51 
52 public:
53  ODRHash() {}
54 
55  // Use this for ODR checking classes between modules. This method compares
56  // more information than the AddDecl class.
57  void AddCXXRecordDecl(const CXXRecordDecl *Record);
58 
59  // Use this for ODR checking functions between modules. This method compares
60  // more information than the AddDecl class. SkipBody will process the
61  // hash as if the function has no body.
62  void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
63 
64  // Use this for ODR checking enums between modules. This method compares
65  // more information than the AddDecl class.
66  void AddEnumDecl(const EnumDecl *Enum);
67 
68  // Process SubDecls of the main Decl. This method calls the DeclVisitor
69  // while AddDecl does not.
70  void AddSubDecl(const Decl *D);
71 
72  // Reset the object for reuse.
73  void clear();
74 
75  // Add booleans to ID and uses it to calculate the hash.
76  unsigned CalculateHash();
77 
78  // Add AST nodes that need to be processed.
79  void AddDecl(const Decl *D);
80  void AddType(const Type *T);
81  void AddQualType(QualType T);
82  void AddStmt(const Stmt *S);
83  void AddIdentifierInfo(const IdentifierInfo *II);
85  void AddTemplateName(TemplateName Name);
86  void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
89 
90  // Save booleans until the end to lower the size of data to process.
91  void AddBoolean(bool value);
92 
93  static bool isWhitelistedDecl(const Decl* D, const DeclContext *Parent);
94 
95 private:
96  void AddDeclarationNameImpl(DeclarationName Name);
97 };
98 
99 } // end namespace clang
100 
101 #endif
Represents a function declaration or definition.
Definition: Decl.h:1717
A (possibly-)qualified type.
Definition: Type.h:642
void AddBoolean(bool value)
Definition: ODRHash.cpp:1049
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl=false)
Definition: ODRHash.cpp:35
Stmt - This represents one statement.
Definition: Stmt.h:66
void AddQualType(QualType T)
Definition: ODRHash.cpp:1040
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
The base class of the type hierarchy.
Definition: Type.h:1415
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:152
void clear()
Definition: ODRHash.cpp:193
One of these records is kept for each identifier that is lexed.
void AddTemplateParameterList(const TemplateParameterList *TPL)
Definition: ODRHash.cpp:184
void AddDecl(const Decl *D)
Definition: ODRHash.cpp:620
void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody=false)
Definition: ODRHash.cpp:509
void AddTemplateName(TemplateName Name)
Definition: ODRHash.cpp:134
NodeId Parent
Definition: ASTDiff.cpp:192
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
void AddType(const Type *T)
Definition: ODRHash.cpp:1035
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:105
void AddSubDecl(const Decl *D)
Definition: ODRHash.cpp:456
void AddEnumDecl(const EnumDecl *Enum)
Definition: ODRHash.cpp:592
Represents a template argument.
Definition: TemplateBase.h:51
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1268
void AddCXXRecordDecl(const CXXRecordDecl *Record)
Definition: ODRHash.cpp:462
The name of a declaration.
Represents an enum.
Definition: Decl.h:3303
unsigned CalculateHash()
Definition: ODRHash.cpp:199
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
void AddStmt(const Stmt *S)
Definition: ODRHash.cpp:25
static bool isWhitelistedDecl(const Decl *D, const DeclContext *Parent)
Definition: ODRHash.cpp:433