clang 22.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"
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
26namespace clang {
27
28class APValue;
29class Decl;
30class IdentifierInfo;
31class NestedNameSpecifier;
32class Stmt;
33class 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.
42class 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
52public:
54
55 // Use this for ODR checking classes between modules. This method compares
56 // more information than the AddDecl class.
58
59 // Use this for ODR checking records in C/Objective-C between modules. This
60 // method compares more information than the AddDecl class.
61 void AddRecordDecl(const RecordDecl *Record);
62
63 // Use this for ODR checking ObjC interfaces. This
64 // method compares more information than the AddDecl class.
66
67 // Use this for ODR checking functions between modules. This method compares
68 // more information than the AddDecl class. SkipBody will process the
69 // hash as if the function has no body.
70 void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody = false);
71
72 // Use this for ODR checking enums between modules. This method compares
73 // more information than the AddDecl class.
74 void AddEnumDecl(const EnumDecl *Enum);
75
76 // Use this for ODR checking ObjC protocols. This
77 // method compares more information than the AddDecl class.
79
80 // Process SubDecls of the main Decl. This method calls the DeclVisitor
81 // while AddDecl does not.
82 void AddSubDecl(const Decl *D);
83
84 // Reset the object for reuse.
85 void clear();
86
87 // Add booleans to ID and uses it to calculate the hash.
88 unsigned CalculateHash();
89
90 // Add AST nodes that need to be processed.
91 void AddDecl(const Decl *D);
92 void AddType(const Type *T);
94 void AddStmt(const Stmt *S);
95 void AddIdentifierInfo(const IdentifierInfo *II);
100 bool TreatAsDecl = false);
101 void AddDeclarationName(DeclarationName Name, bool TreatAsDecl = false) {
103 TreatAsDecl);
104 }
105
108
109 // Save booleans until the end to lower the size of data to process.
110 void AddBoolean(bool value);
111
112 void AddStructuralValue(const APValue &);
113
114 static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent);
115
116private:
117 void AddDeclarationNameInfoImpl(DeclarationNameInfo NameInfo);
118};
119
120} // end namespace clang
121
122#endif
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
llvm::MachO::Record Record
Definition: MachO.h:31
C Language Family Type Representation.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1449
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The name of a declaration.
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:590
Represents an enum.
Definition: Decl.h:4004
Represents a function declaration or definition.
Definition: Decl.h:1999
One of these records is kept for each identifier that is lexed.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
void AddDecl(const Decl *D)
Definition: ODRHash.cpp:816
void AddStmt(const Stmt *S)
Definition: ODRHash.cpp:23
void AddStructuralValue(const APValue &)
Definition: ODRHash.cpp:1268
void AddDeclarationNameInfo(DeclarationNameInfo NameInfo, bool TreatAsDecl=false)
Definition: ODRHash.cpp:33
void AddCXXRecordDecl(const CXXRecordDecl *Record)
Definition: ODRHash.cpp:578
void AddDeclarationName(DeclarationName Name, bool TreatAsDecl=false)
Definition: ODRHash.h:101
void clear()
Definition: ODRHash.cpp:225
void AddIdentifierInfo(const IdentifierInfo *II)
Definition: ODRHash.cpp:28
void AddObjCProtocolDecl(const ObjCProtocolDecl *P)
Definition: ODRHash.cpp:791
void AddObjCInterfaceDecl(const ObjCInterfaceDecl *Record)
Definition: ODRHash.cpp:643
void AddType(const Type *T)
Definition: ODRHash.cpp:1250
void AddEnumDecl(const EnumDecl *Enum)
Definition: ODRHash.cpp:763
void AddDependentTemplateName(const DependentTemplateStorage &Name)
Definition: ODRHash.cpp:134
void AddFunctionDecl(const FunctionDecl *Function, bool SkipBody=false)
Definition: ODRHash.cpp:670
void AddBoolean(bool value)
Definition: ODRHash.cpp:1264
void AddTemplateName(TemplateName Name)
Definition: ODRHash.cpp:143
void AddRecordDecl(const RecordDecl *Record)
Definition: ODRHash.cpp:625
void AddSubDecl(const Decl *D)
Definition: ODRHash.cpp:572
void AddNestedNameSpecifier(NestedNameSpecifier NNS)
Definition: ODRHash.cpp:114
void AddQualType(QualType T)
Definition: ODRHash.cpp:1255
void AddTemplateParameterList(const TemplateParameterList *TPL)
Definition: ODRHash.cpp:216
void AddTemplateArgument(TemplateArgument TA)
Definition: ODRHash.cpp:174
unsigned CalculateHash()
Definition: ODRHash.cpp:231
static bool isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent)
Definition: ODRHash.cpp:546
Represents an ObjC class declaration.
Definition: DeclObjC.h:1154
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2084
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a struct/union/class.
Definition: Decl.h:4309
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:85
Represents a template argument.
Definition: TemplateBase.h:61
Represents a C++ template name within the type system.
Definition: TemplateName.h:222
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:74
The base class of the type hierarchy.
Definition: TypeBase.h:1833
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
Definition: ModuleMapFile.h:36
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ Enum
The "enum" keyword introduces the elaborated-type-specifier.
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...