Skip to content

Commit 6a24e80

Browse files
author
Erich Keane
committed
[NFCI]Create CommonAttributeInfo Type as base type of *Attr and ParsedAttr.
In order to enable future improvements to our attribute diagnostics, this moves info from ParsedAttr into CommonAttributeInfo, then makes this type the base of the *Attr and ParsedAttr types. Quite a bit of refactoring took place, including removing a bunch of redundant Spelling Index propogation. Differential Revision: https://reviews.llvm.org/D67368 llvm-svn: 371875
1 parent 7ff9a93 commit 6a24e80

30 files changed

+1082
-1201
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/Expr.h"
2020
#include "clang/AST/Type.h"
2121
#include "clang/Basic/AttrKinds.h"
22+
#include "clang/Basic/AttributeCommonInfo.h"
2223
#include "clang/Basic/LLVM.h"
2324
#include "clang/Basic/OpenMPKinds.h"
2425
#include "clang/Basic/Sanitizers.h"
@@ -32,6 +33,7 @@
3233

3334
namespace clang {
3435
class ASTContext;
36+
class AttributeCommonInfo;
3537
class IdentifierInfo;
3638
class ObjCInterfaceDecl;
3739
class Expr;
@@ -40,84 +42,79 @@ namespace clang {
4042
class TypeSourceInfo;
4143

4244
/// Attr - This represents one attribute.
43-
class Attr {
44-
private:
45-
SourceRange Range;
46-
unsigned AttrKind : 16;
47-
48-
protected:
49-
/// An index into the spelling list of an
50-
/// attribute defined in Attr.td file.
51-
unsigned SpellingListIndex : 4;
52-
unsigned Inherited : 1;
53-
unsigned IsPackExpansion : 1;
54-
unsigned Implicit : 1;
55-
// FIXME: These are properties of the attribute kind, not state for this
56-
// instance of the attribute.
57-
unsigned IsLateParsed : 1;
58-
unsigned InheritEvenIfAlreadyPresent : 1;
59-
60-
void *operator new(size_t bytes) noexcept {
61-
llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62-
}
63-
void operator delete(void *data) noexcept {
64-
llvm_unreachable("Attrs cannot be released with regular 'delete'.");
65-
}
66-
67-
public:
68-
// Forward so that the regular new and delete do not hide global ones.
69-
void *operator new(size_t Bytes, ASTContext &C,
70-
size_t Alignment = 8) noexcept {
71-
return ::operator new(Bytes, C, Alignment);
72-
}
73-
void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
74-
return ::operator delete(Ptr, C, Alignment);
75-
}
45+
class Attr : public AttributeCommonInfo {
46+
private:
47+
unsigned AttrKind : 16;
48+
49+
protected:
50+
/// An index into the spelling list of an
51+
/// attribute defined in Attr.td file.
52+
unsigned Inherited : 1;
53+
unsigned IsPackExpansion : 1;
54+
unsigned Implicit : 1;
55+
// FIXME: These are properties of the attribute kind, not state for this
56+
// instance of the attribute.
57+
unsigned IsLateParsed : 1;
58+
unsigned InheritEvenIfAlreadyPresent : 1;
59+
60+
void *operator new(size_t bytes) noexcept {
61+
llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62+
}
63+
void operator delete(void *data) noexcept {
64+
llvm_unreachable("Attrs cannot be released with regular 'delete'.");
65+
}
7666

77-
protected:
78-
Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
79-
bool IsLateParsed)
80-
: Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
81-
Inherited(false), IsPackExpansion(false), Implicit(false),
82-
IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
67+
public:
68+
// Forward so that the regular new and delete do not hide global ones.
69+
void *operator new(size_t Bytes, ASTContext &C,
70+
size_t Alignment = 8) noexcept {
71+
return ::operator new(Bytes, C, Alignment);
72+
}
73+
void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
74+
return ::operator delete(Ptr, C, Alignment);
75+
}
8376

84-
public:
77+
protected:
78+
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
79+
attr::Kind AK, bool IsLateParsed)
80+
: AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
81+
IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed),
82+
InheritEvenIfAlreadyPresent(false) {}
8583

86-
attr::Kind getKind() const {
87-
return static_cast<attr::Kind>(AttrKind);
88-
}
84+
public:
85+
attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
8986

90-
unsigned getSpellingListIndex() const { return SpellingListIndex; }
91-
const char *getSpelling() const;
87+
unsigned getSpellingListIndex() const {
88+
return getAttributeSpellingListIndex();
89+
}
90+
const char *getSpelling() const;
9291

93-
SourceLocation getLocation() const { return Range.getBegin(); }
94-
SourceRange getRange() const { return Range; }
95-
void setRange(SourceRange R) { Range = R; }
92+
SourceLocation getLocation() const { return getRange().getBegin(); }
9693

97-
bool isInherited() const { return Inherited; }
94+
bool isInherited() const { return Inherited; }
9895

99-
/// Returns true if the attribute has been implicitly created instead
100-
/// of explicitly written by the user.
101-
bool isImplicit() const { return Implicit; }
102-
void setImplicit(bool I) { Implicit = I; }
96+
/// Returns true if the attribute has been implicitly created instead
97+
/// of explicitly written by the user.
98+
bool isImplicit() const { return Implicit; }
99+
void setImplicit(bool I) { Implicit = I; }
103100

104-
void setPackExpansion(bool PE) { IsPackExpansion = PE; }
105-
bool isPackExpansion() const { return IsPackExpansion; }
101+
void setPackExpansion(bool PE) { IsPackExpansion = PE; }
102+
bool isPackExpansion() const { return IsPackExpansion; }
106103

107-
// Clone this attribute.
108-
Attr *clone(ASTContext &C) const;
104+
// Clone this attribute.
105+
Attr *clone(ASTContext &C) const;
109106

110-
bool isLateParsed() const { return IsLateParsed; }
107+
bool isLateParsed() const { return IsLateParsed; }
111108

112-
// Pretty print this attribute.
113-
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
114-
};
109+
// Pretty print this attribute.
110+
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
111+
};
115112

116113
class TypeAttr : public Attr {
117114
protected:
118-
TypeAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
119-
bool IsLateParsed)
120-
: Attr(AK, R, SpellingListIndex, IsLateParsed) {}
115+
TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
116+
attr::Kind AK, bool IsLateParsed)
117+
: Attr(Context, CommonInfo, AK, IsLateParsed) {}
121118

122119
public:
123120
static bool classof(const Attr *A) {
@@ -128,9 +125,9 @@ class TypeAttr : public Attr {
128125

129126
class StmtAttr : public Attr {
130127
protected:
131-
StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
132-
bool IsLateParsed)
133-
: Attr(AK, R, SpellingListIndex, IsLateParsed) {}
128+
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
129+
attr::Kind AK, bool IsLateParsed)
130+
: Attr(Context, CommonInfo, AK, IsLateParsed) {}
134131

135132
public:
136133
static bool classof(const Attr *A) {
@@ -141,9 +138,10 @@ class StmtAttr : public Attr {
141138

142139
class InheritableAttr : public Attr {
143140
protected:
144-
InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
145-
bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
146-
: Attr(AK, R, SpellingListIndex, IsLateParsed) {
141+
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
142+
attr::Kind AK, bool IsLateParsed,
143+
bool InheritEvenIfAlreadyPresent)
144+
: Attr(Context, CommonInfo, AK, IsLateParsed) {
147145
this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
148146
}
149147

@@ -165,9 +163,10 @@ class InheritableAttr : public Attr {
165163

166164
class InheritableParamAttr : public InheritableAttr {
167165
protected:
168-
InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
166+
InheritableParamAttr(ASTContext &Context,
167+
const AttributeCommonInfo &CommonInfo, attr::Kind AK,
169168
bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
170-
: InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
169+
: InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
171170
InheritEvenIfAlreadyPresent) {}
172171

173172
public:
@@ -182,11 +181,11 @@ class InheritableParamAttr : public InheritableAttr {
182181
/// for the parameter.
183182
class ParameterABIAttr : public InheritableParamAttr {
184183
protected:
185-
ParameterABIAttr(attr::Kind AK, SourceRange R,
186-
unsigned SpellingListIndex, bool IsLateParsed,
184+
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
185+
attr::Kind AK, bool IsLateParsed,
187186
bool InheritEvenIfAlreadyPresent)
188-
: InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
189-
InheritEvenIfAlreadyPresent) {}
187+
: InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
188+
InheritEvenIfAlreadyPresent) {}
190189

191190
public:
192191
ParameterABI getABI() const {

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,7 +3042,7 @@ def LoopHint : Attr {
30423042
}
30433043

30443044
void printPrettyPragma(raw_ostream &OS, const PrintingPolicy &Policy) const {
3045-
unsigned SpellingIndex = getSpellingListIndex();
3045+
unsigned SpellingIndex = getAttributeSpellingListIndex();
30463046
// For "#pragma unroll" and "#pragma nounroll" the string "unroll" or
30473047
// "nounroll" is already emitted as the pragma name.
30483048
if (SpellingIndex == Pragma_nounroll || SpellingIndex == Pragma_nounroll_and_jam)
@@ -3078,7 +3078,7 @@ def LoopHint : Attr {
30783078

30793079
// Return a string suitable for identifying this attribute in diagnostics.
30803080
std::string getDiagnosticName(const PrintingPolicy &Policy) const {
3081-
unsigned SpellingIndex = getSpellingListIndex();
3081+
unsigned SpellingIndex = getAttributeSpellingListIndex();
30823082
if (SpellingIndex == Pragma_nounroll)
30833083
return "#pragma nounroll";
30843084
else if (SpellingIndex == Pragma_unroll)

0 commit comments

Comments
 (0)