Skip to content

Commit 5351a55

Browse files
author
Jenkins
committed
merge main into amd-staging
Change-Id: I1710a38bf184f16d8763fccc0ccafd7242c189a1
2 parents 73960c3 + b11a703 commit 5351a55

File tree

190 files changed

+2542
-1507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+2542
-1507
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ Improvements to Clang's diagnostics
293293
- The lifetimebound and GSL analysis in clang are coherent, allowing clang to
294294
detect more use-after-free bugs. (#GH100549).
295295

296+
- Clang now diagnoses dangling cases where a gsl-pointer is constructed from a gsl-owner object inside a container (#GH100384).
297+
296298
- Clang now warns for u8 character literals used in C23 with ``-Wpre-c23-compat`` instead of ``-Wpre-c++17-compat``.
297299

298300
Improvements to Clang's time-trace

clang/include/clang/AST/TypeLoc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,20 @@ class HLSLAttributedResourceTypeLoc
951951
HLSLAttributedResourceLocInfo> {
952952
public:
953953
TypeLoc getWrappedLoc() const { return getInnerTypeLoc(); }
954+
955+
TypeLoc getContainedLoc() const {
956+
return TypeLoc(getTypePtr()->getContainedType(), getNonLocalData());
957+
}
958+
954959
void setSourceRange(const SourceRange &R) { getLocalData()->Range = R; }
955960
SourceRange getLocalSourceRange() const { return getLocalData()->Range; }
956961
void initializeLocal(ASTContext &Context, SourceLocation loc) {
957962
setSourceRange(SourceRange());
958963
}
959964
QualType getInnerType() const { return getTypePtr()->getWrappedType(); }
965+
unsigned getLocalDataSize() const {
966+
return sizeof(HLSLAttributedResourceLocInfo);
967+
}
960968
};
961969

962970
struct ObjCObjectTypeLocInfo {

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,16 +4643,14 @@ def HLSLResource : InheritableAttr {
46434643
let Documentation = [InternalOnly];
46444644
}
46454645

4646-
def HLSLROV : InheritableAttr {
4646+
def HLSLROV : TypeAttr {
46474647
let Spellings = [CXX11<"hlsl", "is_rov">];
4648-
let Subjects = SubjectList<[Struct]>;
46494648
let LangOpts = [HLSL];
46504649
let Documentation = [InternalOnly];
46514650
}
46524651

4653-
def HLSLResourceClass : InheritableAttr {
4652+
def HLSLResourceClass : TypeAttr {
46544653
let Spellings = [CXX11<"hlsl", "resource_class">];
4655-
let Subjects = SubjectList<[Field]>;
46564654
let LangOpts = [HLSL];
46574655
let Args = [
46584656
EnumArgument<"ResourceClass", "llvm::hlsl::ResourceClass",

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12369,6 +12369,7 @@ def err_hlsl_packoffset_cross_reg_boundary : Error<"packoffset cannot cross regi
1236912369
def err_hlsl_packoffset_alignment_mismatch : Error<"packoffset at 'y' not match alignment %0 required by %1">;
1237012370
def err_hlsl_pointers_unsupported : Error<
1237112371
"%select{pointers|references}0 are unsupported in HLSL">;
12372+
def err_hlsl_missing_resource_class : Error<"HLSL resource needs to have [[hlsl::resource_class()]] attribute">;
1237212373

1237312374
def err_hlsl_operator_unsupported : Error<
1237412375
"the '%select{&|*|->}0' operator is unsupported in HLSL">;

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
#include "clang/AST/ASTFwd.h"
1717
#include "clang/AST/Attr.h"
18+
#include "clang/AST/Type.h"
1819
#include "clang/Basic/SourceLocation.h"
1920
#include "clang/Sema/SemaBase.h"
21+
#include "llvm/ADT/SmallVector.h"
2022
#include "llvm/TargetParser/Triple.h"
2123
#include <initializer_list>
2224

@@ -26,6 +28,12 @@ class IdentifierInfo;
2628
class ParsedAttr;
2729
class Scope;
2830

31+
// FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
32+
// longer need to create builtin buffer types in HLSLExternalSemaSource.
33+
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped,
34+
ArrayRef<const Attr *> AttrList,
35+
QualType &ResType);
36+
2937
class SemaHLSL : public SemaBase {
3038
public:
3139
SemaHLSL(Sema &S);
@@ -59,8 +67,6 @@ class SemaHLSL : public SemaBase {
5967
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
6068
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
6169
void handleShaderAttr(Decl *D, const ParsedAttr &AL);
62-
void handleROVAttr(Decl *D, const ParsedAttr &AL);
63-
void handleResourceClassAttr(Decl *D, const ParsedAttr &AL);
6470
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
6571
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
6672
bool handleResourceTypeAttr(const ParsedAttr &AL);
@@ -78,6 +84,16 @@ class SemaHLSL : public SemaBase {
7884
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg);
7985

8086
QualType getInoutParameterType(QualType Ty);
87+
88+
private:
89+
// HLSL resource type attributes need to be processed all at once.
90+
// This is a list to collect them.
91+
llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
92+
93+
/// SourceLocation corresponding to HLSLAttributedResourceTypeLocs that we
94+
/// have not yet populated.
95+
llvm::DenseMap<const HLSLAttributedResourceType *, SourceLocation>
96+
LocsForHLSLAttributedResources;
8197
};
8298

8399
} // namespace clang

clang/lib/AST/ASTImporter.cpp

Lines changed: 43 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -360,51 +360,42 @@ namespace clang {
360360
}
361361

362362
template <typename TemplateParmDeclT>
363-
void tryUpdateTemplateParmDeclInheritedFrom(NamedDecl *RecentParm,
364-
NamedDecl *NewParm) {
365-
if (auto *ParmT = dyn_cast<TemplateParmDeclT>(RecentParm)) {
366-
if (ParmT->hasDefaultArgument()) {
367-
auto *P = cast<TemplateParmDeclT>(NewParm);
368-
P->removeDefaultArgument();
369-
P->setInheritedDefaultArgument(Importer.ToContext, ParmT);
363+
Error importTemplateParameterDefaultArgument(const TemplateParmDeclT *D,
364+
TemplateParmDeclT *ToD) {
365+
Error Err = Error::success();
366+
if (D->hasDefaultArgument()) {
367+
if (D->defaultArgumentWasInherited()) {
368+
auto *ToInheritedFrom = const_cast<TemplateParmDeclT *>(
369+
importChecked(Err, D->getDefaultArgStorage().getInheritedFrom()));
370+
if (Err)
371+
return Err;
372+
if (!ToInheritedFrom->hasDefaultArgument()) {
373+
// Resolve possible circular dependency between default value of the
374+
// template argument and the template declaration.
375+
const auto ToInheritedDefaultArg =
376+
importChecked(Err, D->getDefaultArgStorage()
377+
.getInheritedFrom()
378+
->getDefaultArgument());
379+
if (Err)
380+
return Err;
381+
ToInheritedFrom->setDefaultArgument(Importer.getToContext(),
382+
ToInheritedDefaultArg);
383+
}
384+
ToD->setInheritedDefaultArgument(ToD->getASTContext(),
385+
ToInheritedFrom);
386+
} else {
387+
Expected<TemplateArgumentLoc> ToDefaultArgOrErr =
388+
import(D->getDefaultArgument());
389+
if (!ToDefaultArgOrErr)
390+
return ToDefaultArgOrErr.takeError();
391+
// Default argument could have been set in the
392+
// '!ToInheritedFrom->hasDefaultArgument()' branch above.
393+
if (!ToD->hasDefaultArgument())
394+
ToD->setDefaultArgument(Importer.getToContext(),
395+
*ToDefaultArgOrErr);
370396
}
371397
}
372-
}
373-
374-
// Update the parameter list `NewParams` of a template declaration
375-
// by "inheriting" default argument values from `RecentParams`,
376-
// which is the parameter list of an earlier declaration of the
377-
// same template. (Note that "inheriting" default argument values
378-
// is not related to object-oriented inheritance.)
379-
//
380-
// In the clang AST template parameters (NonTypeTemplateParmDec,
381-
// TemplateTypeParmDecl, TemplateTemplateParmDecl) have a reference to the
382-
// default value, if one is specified at the first declaration. The default
383-
// value can be specified only once. The template parameters of the
384-
// following declarations have a reference to the original default value
385-
// through the "inherited" value. This value should be set for all imported
386-
// template parameters that have a previous declaration (also a previous
387-
// template declaration).
388-
//
389-
// In the `Visit*ParmDecl` functions the default value of these template
390-
// arguments is always imported. At that location the previous declaration
391-
// is not easily accessible, it is not possible to call
392-
// `setInheritedDefaultArgument` at that place.
393-
// `updateTemplateParametersInheritedFrom` is called later when the already
394-
// imported default value is erased and changed to "inherited".
395-
// It is important to change the mode to "inherited" otherwise false
396-
// structural in-equivalences could be detected.
397-
void updateTemplateParametersInheritedFrom(
398-
const TemplateParameterList &RecentParams,
399-
TemplateParameterList &NewParams) {
400-
for (auto [Idx, Param] : enumerate(RecentParams)) {
401-
tryUpdateTemplateParmDeclInheritedFrom<NonTypeTemplateParmDecl>(
402-
Param, NewParams.getParam(Idx));
403-
tryUpdateTemplateParmDeclInheritedFrom<TemplateTypeParmDecl>(
404-
Param, NewParams.getParam(Idx));
405-
tryUpdateTemplateParmDeclInheritedFrom<TemplateTemplateParmDecl>(
406-
Param, NewParams.getParam(Idx));
407-
}
398+
return Err;
408399
}
409400

410401
public:
@@ -5955,8 +5946,8 @@ ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
59555946
ExpectedDecl
59565947
ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
59575948
// For template arguments, we adopt the translation unit as our declaration
5958-
// context. This context will be fixed when the actual template declaration
5959-
// is created.
5949+
// context. This context will be fixed when (during) the actual template
5950+
// declaration is created.
59605951

59615952
ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
59625953
if (!BeginLocOrErr)
@@ -5988,13 +5979,8 @@ ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
59885979
ToD->setTypeConstraint(ToConceptRef, ToIDC);
59895980
}
59905981

5991-
if (D->hasDefaultArgument()) {
5992-
Expected<TemplateArgumentLoc> ToDefaultArgOrErr =
5993-
import(D->getDefaultArgument());
5994-
if (!ToDefaultArgOrErr)
5995-
return ToDefaultArgOrErr.takeError();
5996-
ToD->setDefaultArgument(ToD->getASTContext(), *ToDefaultArgOrErr);
5997-
}
5982+
if (Error Err = importTemplateParameterDefaultArgument(D, ToD))
5983+
return Err;
59985984

59995985
return ToD;
60005986
}
@@ -6020,13 +6006,9 @@ ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
60206006
D->isParameterPack(), ToTypeSourceInfo))
60216007
return ToD;
60226008

6023-
if (D->hasDefaultArgument()) {
6024-
Expected<TemplateArgumentLoc> ToDefaultArgOrErr =
6025-
import(D->getDefaultArgument());
6026-
if (!ToDefaultArgOrErr)
6027-
return ToDefaultArgOrErr.takeError();
6028-
ToD->setDefaultArgument(Importer.getToContext(), *ToDefaultArgOrErr);
6029-
}
6009+
Err = importTemplateParameterDefaultArgument(D, ToD);
6010+
if (Err)
6011+
return Err;
60306012

60316013
return ToD;
60326014
}
@@ -6057,13 +6039,8 @@ ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
60576039
*TemplateParamsOrErr))
60586040
return ToD;
60596041

6060-
if (D->hasDefaultArgument()) {
6061-
Expected<TemplateArgumentLoc> ToDefaultArgOrErr =
6062-
import(D->getDefaultArgument());
6063-
if (!ToDefaultArgOrErr)
6064-
return ToDefaultArgOrErr.takeError();
6065-
ToD->setDefaultArgument(Importer.getToContext(), *ToDefaultArgOrErr);
6066-
}
6042+
if (Error Err = importTemplateParameterDefaultArgument(D, ToD))
6043+
return Err;
60676044

60686045
return ToD;
60696046
}
@@ -6201,9 +6178,6 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
62016178
}
62026179

62036180
D2->setPreviousDecl(Recent);
6204-
6205-
updateTemplateParametersInheritedFrom(*(Recent->getTemplateParameters()),
6206-
**TemplateParamsOrErr);
62076181
}
62086182

62096183
return D2;
@@ -6518,9 +6492,6 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
65186492
ToTemplated->setPreviousDecl(PrevTemplated);
65196493
}
65206494
ToVarTD->setPreviousDecl(Recent);
6521-
6522-
updateTemplateParametersInheritedFrom(*(Recent->getTemplateParameters()),
6523-
**TemplateParamsOrErr);
65246495
}
65256496

65266497
return ToVarTD;
@@ -6793,9 +6764,6 @@ ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
67936764
TemplatedFD->setPreviousDecl(PrevTemplated);
67946765
}
67956766
ToFunc->setPreviousDecl(Recent);
6796-
6797-
updateTemplateParametersInheritedFrom(*(Recent->getTemplateParameters()),
6798-
*Params);
67996767
}
68006768

68016769
return ToFunc;

clang/lib/AST/TypePrinter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,10 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
19421942
case attr::BTFTypeTag:
19431943
llvm_unreachable("BTFTypeTag attribute handled separately");
19441944

1945+
case attr::HLSLResourceClass:
1946+
case attr::HLSLROV:
1947+
llvm_unreachable("HLSL resource type attributes handled separately");
1948+
19451949
case attr::OpenCLPrivateAddressSpace:
19461950
case attr::OpenCLGlobalAddressSpace:
19471951
case attr::OpenCLGlobalDeviceAddressSpace:
@@ -2062,7 +2066,11 @@ void TypePrinter::printBTFTagAttributedAfter(const BTFTagAttributedType *T,
20622066
void TypePrinter::printHLSLAttributedResourceBefore(
20632067
const HLSLAttributedResourceType *T, raw_ostream &OS) {
20642068
printBefore(T->getWrappedType(), OS);
2069+
}
20652070

2071+
void TypePrinter::printHLSLAttributedResourceAfter(
2072+
const HLSLAttributedResourceType *T, raw_ostream &OS) {
2073+
printAfter(T->getWrappedType(), OS);
20662074
const HLSLAttributedResourceType::Attributes &Attrs = T->getAttrs();
20672075
OS << " [[hlsl::resource_class("
20682076
<< HLSLResourceClassAttr::ConvertResourceClassToStr(Attrs.ResourceClass)
@@ -2071,11 +2079,6 @@ void TypePrinter::printHLSLAttributedResourceBefore(
20712079
OS << " [[hlsl::is_rov()]]";
20722080
}
20732081

2074-
void TypePrinter::printHLSLAttributedResourceAfter(
2075-
const HLSLAttributedResourceType *T, raw_ostream &OS) {
2076-
printAfter(T->getWrappedType(), OS);
2077-
}
2078-
20792082
void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T,
20802083
raw_ostream &OS) {
20812084
OS << T->getDecl()->getName();

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,14 @@ void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
295295
// inside the record decl
296296
for (auto *FD : RD->fields()) {
297297
const auto *HLSLResAttr = FD->getAttr<HLSLResourceAttr>();
298-
const auto *HLSLResClassAttr = FD->getAttr<HLSLResourceClassAttr>();
299-
if (!HLSLResAttr || !HLSLResClassAttr)
298+
const HLSLAttributedResourceType *AttrResType =
299+
dyn_cast<HLSLAttributedResourceType>(FD->getType().getTypePtr());
300+
if (!HLSLResAttr || !AttrResType)
300301
continue;
301302

302-
llvm::hlsl::ResourceClass RC = HLSLResClassAttr->getResourceClass();
303+
llvm::hlsl::ResourceClass RC = AttrResType->getAttrs().ResourceClass;
304+
bool IsROV = AttrResType->getAttrs().IsROV;
303305
llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind();
304-
bool IsROV = FD->hasAttr<HLSLROVAttr>();
305306
llvm::hlsl::ElementType ET = calculateElementType(CGM.getContext(), Ty);
306307

307308
BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>());

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,17 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
403403
visitLocalsRetainedByInitializer(Path, Arg, Visit, true);
404404
Path.pop_back();
405405
};
406-
auto VisitGSLPointerArg = [&](const Decl *D, Expr *Arg, bool Value) {
406+
auto VisitGSLPointerArg = [&](const FunctionDecl *Callee, Expr *Arg) {
407407
// We are not interested in the temporary base objects of gsl Pointers:
408408
// Temp().ptr; // Here ptr might not dangle.
409409
if (isa<MemberExpr>(Arg->IgnoreImpCasts()))
410410
return;
411-
// Once we initialized a value with a reference, it can no longer dangle.
412-
if (!Value) {
411+
auto ReturnType = Callee->getReturnType();
412+
413+
// Once we initialized a value with a non gsl-owner reference, it can no
414+
// longer dangle.
415+
if (ReturnType->isReferenceType() &&
416+
!isRecordWithAttr<OwnerAttr>(ReturnType->getPointeeType())) {
413417
for (const IndirectLocalPathEntry &PE : llvm::reverse(Path)) {
414418
if (PE.Kind == IndirectLocalPathEntry::GslReferenceInit ||
415419
PE.Kind == IndirectLocalPathEntry::LifetimeBoundCall)
@@ -420,9 +424,10 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
420424
break;
421425
}
422426
}
423-
Path.push_back({Value ? IndirectLocalPathEntry::GslPointerInit
424-
: IndirectLocalPathEntry::GslReferenceInit,
425-
Arg, D});
427+
Path.push_back({ReturnType->isReferenceType()
428+
? IndirectLocalPathEntry::GslReferenceInit
429+
: IndirectLocalPathEntry::GslPointerInit,
430+
Arg, Callee});
426431
if (Arg->isGLValue())
427432
visitLocalsRetainedByReferenceBinding(Path, Arg, RK_ReferenceBinding,
428433
Visit);
@@ -453,8 +458,7 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
453458
else if (EnableGSLAnalysis) {
454459
if (auto *CME = dyn_cast<CXXMethodDecl>(Callee);
455460
CME && shouldTrackImplicitObjectArg(CME))
456-
VisitGSLPointerArg(Callee, ObjectArg,
457-
!Callee->getReturnType()->isReferenceType());
461+
VisitGSLPointerArg(Callee, ObjectArg);
458462
}
459463
}
460464

@@ -465,13 +469,11 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
465469
VisitLifetimeBoundArg(Callee->getParamDecl(I), Args[I]);
466470
else if (EnableGSLAnalysis && I == 0) {
467471
if (shouldTrackFirstArgument(Callee)) {
468-
VisitGSLPointerArg(Callee, Args[0],
469-
!Callee->getReturnType()->isReferenceType());
472+
VisitGSLPointerArg(Callee, Args[0]);
470473
} else if (auto *CCE = dyn_cast<CXXConstructExpr>(Call);
471474
CCE &&
472475
CCE->getConstructor()->getParent()->hasAttr<PointerAttr>()) {
473-
VisitGSLPointerArg(CCE->getConstructor()->getParamDecl(0), Args[0],
474-
true);
476+
VisitGSLPointerArg(CCE->getConstructor(), Args[0]);
475477
}
476478
}
477479
}

0 commit comments

Comments
 (0)