clang  16.0.0git
ODRHash.h
Go to the documentation of this file.
1 //===-- ODRHash.h - Hashing to diagnose ODR failures ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the declaration of the ODRHash class, which calculates
11 /// a hash based on AST nodes, which is stable across different runs.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_AST_ODRHASH_H
16 #define LLVM_CLANG_AST_ODRHASH_H
17 
19 #include "clang/AST/Type.h"
20 #include "clang/AST/TemplateBase.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/SmallVector.h"
25 
26 namespace clang {
27 
28 class Decl;
29 class IdentifierInfo;
30 class NestedNameSpecifier;
31 class Stmt;
32 class TemplateParameterList;
33 
34 // ODRHash is used to calculate a hash based on AST node contents that
35 // does not rely on pointer addresses. This allows the hash to not vary
36 // between runs and is usable to detect ODR problems in modules. To use,
37 // construct an ODRHash object, then call Add* methods over the nodes that
38 // need to be hashed. Then call CalculateHash to get the hash value.
39 // Typically, only one Add* call is needed. clear can be called to reuse the
40 // object.
41 class ODRHash {
42  // Use DenseMaps to convert from DeclarationName and Type pointers
43  // to an index value.
44  llvm::DenseMap<DeclarationName, unsigned> DeclNameMap;
45 
46  // Save space by processing bools at the end.
48 
49  llvm::FoldingSetNodeID ID;
50 
51 public:
52  ODRHash() {}
53 
54  // Use this for ODR checking classes between modules. This method compares
55  // more information than the AddDecl class.
56  void AddCXXRecordDecl(const CXXRecordDecl *Record);
57 
58  // Use this for ODR checking functions between modules. This method compares
59  // more information than the AddDecl class. SkipBody will process the
60  // hash as if the function has no body.
61  void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
62 
63  // Use this for ODR checking enums between modules. This method compares
64  // more information than the AddDecl class.
65  void AddEnumDecl(const EnumDecl *Enum);
66 
67  // Use this for ODR checking ObjC protocols. This
68  // method compares more information than the AddDecl class.
70 
71  // Process SubDecls of the main Decl. This method calls the DeclVisitor
72  // while AddDecl does not.
73  void AddSubDecl(const Decl *D);
74 
75  // Reset the object for reuse.
76  void clear();
77 
78  // Add booleans to ID and uses it to calculate the hash.
79  unsigned CalculateHash();
80 
81  // Add AST nodes that need to be processed.
82  void AddDecl(const Decl *D);
83  void AddType(const Type *T);
84  void AddQualType(QualType T);
85  void AddStmt(const Stmt *S);
86  void AddIdentifierInfo(const IdentifierInfo *II);
88  void AddTemplateName(TemplateName Name);
89  void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false);
92 
93  // Save booleans until the end to lower the size of data to process.
94  void AddBoolean(bool value);
95 
96  static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent);
97 
98 private:
99  void AddDeclarationNameImpl(DeclarationName Name);
100 };
101 
102 } // end namespace clang
103 
104 #endif
clang::ODRHash::AddDecl
void AddDecl(const Decl *D)
Definition: ODRHash.cpp:728
clang::ODRHash::ODRHash
ODRHash()
Definition: ODRHash.h:52
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1389
clang::ODRHash::AddBoolean
void AddBoolean(bool value)
Definition: ODRHash.cpp:1231
llvm::SmallVector< bool, 128 >
clang::ODRHash::AddDeclarationName
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl=false)
Definition: ODRHash.cpp:34
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::NestedNameSpecifier
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
Definition: NestedNameSpecifier.h:50
clang::ODRHash::clear
void clear()
Definition: ODRHash.cpp:202
clang::ODRHash::AddTemplateParameterList
void AddTemplateParameterList(const TemplateParameterList *TPL)
Definition: ODRHash.cpp:193
clang::DeclarationName
The name of a declaration.
Definition: DeclarationName.h:144
clang::EnumDecl
Represents an enum.
Definition: Decl.h:3705
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1565
clang::CodeGen::AlignmentSource::Decl
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::ODRHash::isSubDeclToBeProcessed
static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent)
Definition: ODRHash.cpp:514
TemplateBase.h
clang::ODRHash::AddQualType
void AddQualType(QualType T)
Definition: ODRHash.cpp:1222
clang::ODRHash::AddFunctionDecl
void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody=false)
Definition: ODRHash.cpp:592
clang::TemplateArgument
Represents a template argument.
Definition: TemplateBase.h:61
clang::ODRHash::AddTemplateName
void AddTemplateName(TemplateName Name)
Definition: ODRHash.cpp:141
Type.h
clang::ODRHash::AddIdentifierInfo
void AddIdentifierInfo(const IdentifierInfo *II)
Definition: ODRHash.cpp:29
clang::ODRHash::AddTemplateArgument
void AddTemplateArgument(TemplateArgument TA)
Definition: ODRHash.cpp:161
clang::TemplateParameterList
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:70
clang::ODRHash
Definition: ODRHash.h:41
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
clang::RISCV::TA
@ TA
Definition: RISCVVIntrinsicUtils.h:98
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::TemplateName
Represents a C++ template name within the type system.
Definition: TemplateName.h:201
clang::ODRHash::AddType
void AddType(const Type *T)
Definition: ODRHash.cpp:1217
clang::ODRHash::AddNestedNameSpecifier
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS)
Definition: ODRHash.cpp:112
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::ODRHash::AddCXXRecordDecl
void AddCXXRecordDecl(const CXXRecordDecl *Record)
Definition: ODRHash.cpp:545
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:85
DeclarationName.h
clang::ODRHash::AddStmt
void AddStmt(const Stmt *S)
Definition: ODRHash.cpp:24
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:71
clang::ODRHash::AddObjCProtocolDecl
void AddObjCProtocolDecl(const ObjCProtocolDecl *P)
Definition: ODRHash.cpp:703
clang::ObjCProtocolDecl
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2050
clang::ODRHash::AddSubDecl
void AddSubDecl(const Decl *D)
Definition: ODRHash.cpp:539
clang::ODRHash::AddEnumDecl
void AddEnumDecl(const EnumDecl *Enum)
Definition: ODRHash.cpp:675
Parent
NodeId Parent
Definition: ASTDiff.cpp:190
clang::ODRHash::CalculateHash
unsigned CalculateHash()
Definition: ODRHash.cpp:208
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1904