Skip to content

Commit 8804ce9

Browse files
committed
[BoundsSafety] build TypeLoc for CountAttributedType
This adds attribute SourceRange to CountAttributedTypeLoc and populates it. The following commit will change diagnostics to use this information. Fixes #113582
1 parent 3bb903e commit 8804ce9

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

clang/include/clang/AST/TypeLoc.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/NestedNameSpecifierBase.h"
2020
#include "clang/AST/TemplateBase.h"
2121
#include "clang/AST/TypeBase.h"
22+
#include "clang/Basic/IdentifierTable.h"
2223
#include "clang/Basic/LLVM.h"
2324
#include "clang/Basic/SourceLocation.h"
2425
#include "clang/Basic/Specifiers.h"
@@ -1303,18 +1304,24 @@ class ObjCInterfaceTypeLoc : public ConcreteTypeLoc<ObjCObjectTypeLoc,
13031304
}
13041305
};
13051306

1306-
struct BoundsAttributedLocInfo {};
1307+
struct BoundsAttributedLocInfo {
1308+
SourceRange Range;
1309+
};
13071310
class BoundsAttributedTypeLoc
13081311
: public ConcreteTypeLoc<UnqualTypeLoc, BoundsAttributedTypeLoc,
13091312
BoundsAttributedType, BoundsAttributedLocInfo> {
13101313
public:
13111314
TypeLoc getInnerLoc() const { return getInnerTypeLoc(); }
13121315
QualType getInnerType() const { return getTypePtr()->desugar(); }
13131316
void initializeLocal(ASTContext &Context, SourceLocation Loc) {
1314-
// nothing to do
1317+
setAttrRange({Loc, Loc});
1318+
}
1319+
void setAttrRange(SourceRange Range) {
1320+
getLocalData()->Range = Range;
13151321
}
1316-
// LocalData is empty and TypeLocBuilder doesn't handle DataSize 1.
1317-
unsigned getLocalDataSize() const { return 0; }
1322+
SourceRange getAttrRange() const { return getLocalData()->Range; }
1323+
1324+
unsigned getLocalDataSize() const { return sizeof(BoundsAttributedLocInfo); }
13181325
};
13191326

13201327
class CountAttributedTypeLoc final
@@ -1325,8 +1332,6 @@ class CountAttributedTypeLoc final
13251332
Expr *getCountExpr() const { return getTypePtr()->getCountExpr(); }
13261333
bool isCountInBytes() const { return getTypePtr()->isCountInBytes(); }
13271334
bool isOrNull() const { return getTypePtr()->isOrNull(); }
1328-
1329-
SourceRange getLocalSourceRange() const;
13301335
};
13311336

13321337
struct MacroQualifiedLocInfo {

clang/lib/AST/TypeLoc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,6 @@ SourceRange AttributedTypeLoc::getLocalSourceRange() const {
590590
return getAttr() ? getAttr()->getRange() : SourceRange();
591591
}
592592

593-
SourceRange CountAttributedTypeLoc::getLocalSourceRange() const {
594-
return getCountExpr() ? getCountExpr()->getSourceRange() : SourceRange();
595-
}
596-
597593
SourceRange BTFTagAttributedTypeLoc::getLocalSourceRange() const {
598594
return getAttr() ? getAttr()->getRange() : SourceRange();
599595
}

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "TypeLocBuilder.h"
1314
#include "clang/AST/APValue.h"
1415
#include "clang/AST/ASTConsumer.h"
1516
#include "clang/AST/ASTContext.h"
1617
#include "clang/AST/ASTMutationListener.h"
18+
#include "clang/AST/Attr.h"
1719
#include "clang/AST/CXXInheritance.h"
1820
#include "clang/AST/Decl.h"
1921
#include "clang/AST/DeclCXX.h"
@@ -24,6 +26,8 @@
2426
#include "clang/AST/ExprCXX.h"
2527
#include "clang/AST/Mangle.h"
2628
#include "clang/AST/Type.h"
29+
#include "clang/AST/TypeBase.h"
30+
#include "clang/AST/TypeLoc.h"
2731
#include "clang/Basic/CharInfo.h"
2832
#include "clang/Basic/Cuda.h"
2933
#include "clang/Basic/DarwinSDKInfo.h"
@@ -6580,9 +6584,14 @@ static void handleCountedByAttrField(Sema &S, Decl *D, const ParsedAttr &AL) {
65806584
if (S.CheckCountedByAttrOnField(FD, CountExpr, CountInBytes, OrNull))
65816585
return;
65826586

6587+
TypeLocBuilder TLB;
65836588
QualType CAT = S.BuildCountAttributedArrayOrPointerType(
65846589
FD->getType(), CountExpr, CountInBytes, OrNull);
6590+
TLB.pushFullCopy(FD->getTypeSourceInfo()->getTypeLoc());
6591+
CountAttributedTypeLoc CATL = TLB.push<CountAttributedTypeLoc>(CAT);
6592+
CATL.setAttrRange(AL.getRange());
65856593
FD->setType(CAT);
6594+
FD->setTypeSourceInfo(TLB.getTypeSourceInfo(S.getASTContext(), CAT));
65866595
}
65876596

65886597
static void handleFunctionReturnThunksAttr(Sema &S, Decl *D,

0 commit comments

Comments
 (0)