Skip to content

Commit b6c4c8d

Browse files
author
z1_cciauto
authored
merge main into amd-staging (llvm#3356)
2 parents c4a7948 + 8e3b6ae commit b6c4c8d

File tree

148 files changed

+5944
-2342
lines changed

Some content is hidden

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

148 files changed

+5944
-2342
lines changed

bolt/lib/Core/Relocation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,15 +1018,15 @@ void Relocation::print(raw_ostream &OS) const {
10181018
OS << "RType:" << Twine::utohexstr(Type);
10191019
break;
10201020

1021-
case Triple::aarch64:
1021+
case Triple::aarch64: {
10221022
static const char *const AArch64RelocNames[] = {
10231023
#define ELF_RELOC(name, value) #name,
10241024
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
10251025
#undef ELF_RELOC
10261026
};
10271027
assert(Type < ArrayRef(AArch64RelocNames).size());
10281028
OS << AArch64RelocNames[Type];
1029-
break;
1029+
} break;
10301030

10311031
case Triple::riscv64:
10321032
// RISC-V relocations are not sequentially numbered so we cannot use an
@@ -1043,15 +1043,15 @@ void Relocation::print(raw_ostream &OS) const {
10431043
}
10441044
break;
10451045

1046-
case Triple::x86_64:
1046+
case Triple::x86_64: {
10471047
static const char *const X86RelocNames[] = {
10481048
#define ELF_RELOC(name, value) #name,
10491049
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
10501050
#undef ELF_RELOC
10511051
};
10521052
assert(Type < ArrayRef(X86RelocNames).size());
10531053
OS << X86RelocNames[Type];
1054-
break;
1054+
} break;
10551055
}
10561056
OS << ", 0x" << Twine::utohexstr(Offset);
10571057
if (Symbol) {

clang-tools-extra/clangd/FindSymbols.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "SourceCode.h"
1515
#include "index/Index.h"
1616
#include "support/Logger.h"
17+
#include "clang/AST/DeclFriend.h"
1718
#include "clang/AST/DeclTemplate.h"
1819
#include "clang/Index/IndexSymbol.h"
1920
#include "llvm/ADT/ArrayRef.h"
@@ -391,6 +392,17 @@ class DocumentOutline {
391392
D = TD;
392393
}
393394

395+
// FriendDecls don't act as DeclContexts, but they might wrap a function
396+
// definition that won't be visible through other means in the AST. Hence
397+
// unwrap it here instead.
398+
if (auto *Friend = llvm::dyn_cast<FriendDecl>(D)) {
399+
if (auto *Func =
400+
llvm::dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl())) {
401+
if (Func->isThisDeclarationADefinition())
402+
D = Func;
403+
}
404+
}
405+
394406
VisitKind Visit = shouldVisit(D);
395407
if (Visit == VisitKind::No)
396408
return;

clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ TEST(DocumentSymbols, BasicSymbols) {
335335
Foo(int a) {}
336336
void $decl[[f]]();
337337
friend void f1();
338+
friend void f2() {}
338339
friend class Friend;
339340
Foo& operator=(const Foo&);
340341
~Foo();
@@ -346,7 +347,7 @@ TEST(DocumentSymbols, BasicSymbols) {
346347
};
347348
348349
void f1();
349-
inline void f2() {}
350+
void f2();
350351
static const int KInt = 2;
351352
const char* kStr = "123";
352353
@@ -386,6 +387,8 @@ TEST(DocumentSymbols, BasicSymbols) {
386387
withDetail("(int)"), children()),
387388
AllOf(withName("f"), withKind(SymbolKind::Method),
388389
withDetail("void ()"), children()),
390+
AllOf(withName("f2"), withKind(SymbolKind::Function),
391+
withDetail("void ()"), children()),
389392
AllOf(withName("operator="), withKind(SymbolKind::Method),
390393
withDetail("Foo &(const Foo &)"), children()),
391394
AllOf(withName("~Foo"), withKind(SymbolKind::Constructor),

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ LLVM_DUMP_METHOD void Block::dump(llvm::raw_ostream &OS) const {
531531
Desc->dump(OS);
532532
OS << ")\n";
533533
unsigned NPointers = 0;
534-
for (const Pointer *P = Pointers; P; P = P->Next) {
534+
for (const Pointer *P = Pointers; P; P = P->asBlockPointer().Next) {
535535
++NPointers;
536536
}
537537
OS << " EvalID: " << EvalID << '\n';

clang/lib/AST/ByteCode/DynamicAllocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void DynamicAllocator::cleanup() {
2727
B->invokeDtor();
2828
if (B->hasPointers()) {
2929
while (B->Pointers) {
30-
Pointer *Next = B->Pointers->Next;
30+
Pointer *Next = B->Pointers->asBlockPointer().Next;
3131
B->Pointers->PointeeStorage.BS.Pointee = nullptr;
3232
B->Pointers = Next;
3333
}

clang/lib/AST/ByteCode/InterpBlock.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ void Block::addPointer(Pointer *P) {
2727
assert(!hasPointer(P));
2828
#endif
2929
if (Pointers)
30-
Pointers->Prev = P;
31-
P->Next = Pointers;
32-
P->Prev = nullptr;
30+
Pointers->PointeeStorage.BS.Prev = P;
31+
P->PointeeStorage.BS.Next = Pointers;
32+
P->PointeeStorage.BS.Prev = nullptr;
3333
Pointers = P;
3434
#ifndef NDEBUG
3535
assert(hasPointer(P));
@@ -48,13 +48,15 @@ void Block::removePointer(Pointer *P) {
4848
assert(hasPointer(P));
4949
#endif
5050

51+
BlockPointer &BP = P->PointeeStorage.BS;
52+
5153
if (Pointers == P)
52-
Pointers = P->Next;
54+
Pointers = BP.Next;
5355

54-
if (P->Prev)
55-
P->Prev->Next = P->Next;
56-
if (P->Next)
57-
P->Next->Prev = P->Prev;
56+
if (BP.Prev)
57+
BP.Prev->PointeeStorage.BS.Next = BP.Next;
58+
if (BP.Next)
59+
BP.Next->PointeeStorage.BS.Prev = BP.Prev;
5860
P->PointeeStorage.BS.Pointee = nullptr;
5961
#ifndef NDEBUG
6062
assert(!hasPointer(P));
@@ -68,7 +70,9 @@ void Block::cleanup() {
6870

6971
void Block::replacePointer(Pointer *Old, Pointer *New) {
7072
assert(Old);
73+
assert(Old->isBlockPointer());
7174
assert(New);
75+
assert(New->isBlockPointer());
7276
assert(Old != New);
7377
if (IsStatic) {
7478
assert(!Pointers);
@@ -78,17 +82,20 @@ void Block::replacePointer(Pointer *Old, Pointer *New) {
7882
assert(hasPointer(Old));
7983
#endif
8084

81-
if (Old->Prev)
82-
Old->Prev->Next = New;
83-
if (Old->Next)
84-
Old->Next->Prev = New;
85-
New->Prev = Old->Prev;
86-
New->Next = Old->Next;
85+
BlockPointer &OldBP = Old->PointeeStorage.BS;
86+
BlockPointer &NewBP = New->PointeeStorage.BS;
87+
88+
if (OldBP.Prev)
89+
OldBP.Prev->PointeeStorage.BS.Next = New;
90+
if (OldBP.Next)
91+
OldBP.Next->PointeeStorage.BS.Prev = New;
92+
NewBP.Prev = OldBP.Prev;
93+
NewBP.Next = OldBP.Next;
8794
if (Pointers == Old)
8895
Pointers = New;
8996

90-
Old->PointeeStorage.BS.Pointee = nullptr;
91-
New->PointeeStorage.BS.Pointee = this;
97+
OldBP.Pointee = nullptr;
98+
NewBP.Pointee = this;
9299
#ifndef NDEBUG
93100
assert(!hasPointer(Old));
94101
assert(hasPointer(New));
@@ -97,7 +104,7 @@ void Block::replacePointer(Pointer *Old, Pointer *New) {
97104

98105
#ifndef NDEBUG
99106
bool Block::hasPointer(const Pointer *P) const {
100-
for (const Pointer *C = Pointers; C; C = C->Next) {
107+
for (const Pointer *C = Pointers; C; C = C->asBlockPointer().Next) {
101108
if (C == P)
102109
return true;
103110
}
@@ -120,7 +127,7 @@ DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
120127

121128
// Transfer pointers.
122129
B.Pointers = Blk->Pointers;
123-
for (Pointer *P = Blk->Pointers; P; P = P->Next)
130+
for (Pointer *P = Blk->Pointers; P; P = P->asBlockPointer().Next)
124131
P->PointeeStorage.BS.Pointee = &B;
125132
Blk->Pointers = nullptr;
126133
}

clang/lib/AST/ByteCode/InterpState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void InterpState::cleanup() {
5252
// As a last resort, make sure all pointers still pointing to a dead block
5353
// don't point to it anymore.
5454
for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) {
55-
for (Pointer *P = DB->B.Pointers; P; P = P->Next) {
55+
for (Pointer *P = DB->B.Pointers; P; P = P->asBlockPointer().Next) {
5656
P->PointeeStorage.BS.Pointee = nullptr;
5757
}
5858
}

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Pointer::Pointer(Block *Pointee, unsigned Base, uint64_t Offset)
4242
: Offset(Offset), StorageKind(Storage::Block) {
4343
assert((Base == RootPtrMark || Base % alignof(void *) == 0) && "wrong base");
4444

45-
PointeeStorage.BS = {Pointee, Base};
45+
PointeeStorage.BS = {Pointee, Base, nullptr, nullptr};
4646

4747
if (Pointee)
4848
Pointee->addPointer(this);
@@ -89,7 +89,6 @@ Pointer &Pointer::operator=(const Pointer &P) {
8989

9090
if (P.isBlockPointer()) {
9191
PointeeStorage.BS = P.PointeeStorage.BS;
92-
PointeeStorage.BS.Pointee = P.PointeeStorage.BS.Pointee;
9392

9493
if (PointeeStorage.BS.Pointee)
9594
PointeeStorage.BS.Pointee->addPointer(this);
@@ -127,7 +126,6 @@ Pointer &Pointer::operator=(Pointer &&P) {
127126

128127
if (P.isBlockPointer()) {
129128
PointeeStorage.BS = P.PointeeStorage.BS;
130-
PointeeStorage.BS.Pointee = P.PointeeStorage.BS.Pointee;
131129

132130
if (PointeeStorage.BS.Pointee)
133131
PointeeStorage.BS.Pointee->addPointer(this);

clang/lib/AST/ByteCode/Pointer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct BlockPointer {
3939
Block *Pointee;
4040
/// Start of the current subfield.
4141
unsigned Base;
42+
/// Previous link in the pointer chain.
43+
Pointer *Prev;
44+
/// Next link in the pointer chain.
45+
Pointer *Next;
4246
};
4347

4448
struct IntPointer {
@@ -832,15 +836,10 @@ class Pointer {
832836
/// Offset into the storage.
833837
uint64_t Offset = 0;
834838

835-
/// Previous link in the pointer chain.
836-
Pointer *Prev = nullptr;
837-
/// Next link in the pointer chain.
838-
Pointer *Next = nullptr;
839-
840839
Storage StorageKind = Storage::Int;
841840
union {
842-
BlockPointer BS;
843841
IntPointer Int;
842+
BlockPointer BS;
844843
FunctionPointer Fn;
845844
TypeidPointer Typeid;
846845
} PointeeStorage;

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Basic/SourceLocation.h"
2626
#include "clang/Lex/Lexer.h"
2727
#include "clang/Lex/Preprocessor.h"
28+
#include "llvm/ADT/APInt.h"
2829
#include "llvm/ADT/APSInt.h"
2930
#include "llvm/ADT/STLFunctionalExtras.h"
3031
#include "llvm/ADT/SmallSet.h"
@@ -809,28 +810,86 @@ static bool hasUnsafeFormatOrSArg(const CallExpr *Call, const Expr *&UnsafeArg,
809810
const CallExpr *Call;
810811
unsigned FmtArgIdx;
811812
const Expr *&UnsafeArg;
813+
ASTContext &Ctx;
814+
815+
// Returns an `Expr` representing the precision if specified, null
816+
// otherwise.
817+
// The parameter `Call` is a printf call and the parameter `Precision` is
818+
// the precision of a format specifier of the `Call`.
819+
//
820+
// For example, for the `printf("%d, %.10s", 10, p)` call
821+
// `Precision` can be the precision of either "%d" or "%.10s". The former
822+
// one will have `NotSpecified` kind.
823+
const Expr *
824+
getPrecisionAsExpr(const analyze_printf::OptionalAmount &Precision,
825+
const CallExpr *Call) {
826+
unsigned PArgIdx = -1;
827+
828+
if (Precision.hasDataArgument())
829+
PArgIdx = Precision.getPositionalArgIndex() + FmtArgIdx;
830+
if (0 < PArgIdx && PArgIdx < Call->getNumArgs()) {
831+
const Expr *PArg = Call->getArg(PArgIdx);
832+
833+
// Strip the cast if `PArg` is a cast-to-int expression:
834+
if (auto *CE = dyn_cast<CastExpr>(PArg);
835+
CE && CE->getType()->isSignedIntegerType())
836+
PArg = CE->getSubExpr();
837+
return PArg;
838+
}
839+
if (Precision.getHowSpecified() ==
840+
analyze_printf::OptionalAmount::HowSpecified::Constant) {
841+
auto SizeTy = Ctx.getSizeType();
842+
llvm::APSInt PArgVal = llvm::APSInt(
843+
llvm::APInt(Ctx.getTypeSize(SizeTy), Precision.getConstantAmount()),
844+
true);
845+
846+
return IntegerLiteral::Create(Ctx, PArgVal, Ctx.getSizeType(), {});
847+
}
848+
return nullptr;
849+
}
812850

813851
public:
814852
StringFormatStringHandler(const CallExpr *Call, unsigned FmtArgIdx,
815-
const Expr *&UnsafeArg)
816-
: Call(Call), FmtArgIdx(FmtArgIdx), UnsafeArg(UnsafeArg) {}
853+
const Expr *&UnsafeArg, ASTContext &Ctx)
854+
: Call(Call), FmtArgIdx(FmtArgIdx), UnsafeArg(UnsafeArg), Ctx(Ctx) {}
817855

818856
bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
819857
const char *startSpecifier,
820858
unsigned specifierLen,
821859
const TargetInfo &Target) override {
822-
if (FS.getConversionSpecifier().getKind() ==
823-
analyze_printf::PrintfConversionSpecifier::sArg) {
824-
unsigned ArgIdx = FS.getPositionalArgIndex() + FmtArgIdx;
825-
826-
if (0 < ArgIdx && ArgIdx < Call->getNumArgs())
827-
if (!isNullTermPointer(Call->getArg(ArgIdx))) {
828-
UnsafeArg = Call->getArg(ArgIdx); // output
829-
// returning false stops parsing immediately
830-
return false;
831-
}
832-
}
833-
return true; // continue parsing
860+
if (FS.getConversionSpecifier().getKind() !=
861+
analyze_printf::PrintfConversionSpecifier::sArg)
862+
return true; // continue parsing
863+
864+
unsigned ArgIdx = FS.getPositionalArgIndex() + FmtArgIdx;
865+
866+
if (!(0 < ArgIdx && ArgIdx < Call->getNumArgs()))
867+
// If the `ArgIdx` is invalid, give up.
868+
return true; // continue parsing
869+
870+
const Expr *Arg = Call->getArg(ArgIdx);
871+
872+
if (isNullTermPointer(Arg))
873+
// If Arg is a null-terminated pointer, it is safe anyway.
874+
return true; // continue parsing
875+
876+
// Otherwise, check if the specifier has a precision and if the character
877+
// pointer is safely bound by the precision:
878+
auto LengthModifier = FS.getLengthModifier();
879+
QualType ArgType = Arg->getType();
880+
bool IsArgTypeValid = // Is ArgType a character pointer type?
881+
ArgType->isPointerType() &&
882+
(LengthModifier.getKind() == LengthModifier.AsWideChar
883+
? ArgType->getPointeeType()->isWideCharType()
884+
: ArgType->getPointeeType()->isCharType());
885+
886+
if (auto *Precision = getPrecisionAsExpr(FS.getPrecision(), Call);
887+
Precision && IsArgTypeValid)
888+
if (isPtrBufferSafe(Arg, Precision, Ctx))
889+
return true;
890+
// Handle unsafe case:
891+
UnsafeArg = Call->getArg(ArgIdx); // output
892+
return false; // returning false stops parsing immediately
834893
}
835894
};
836895

@@ -846,7 +905,7 @@ static bool hasUnsafeFormatOrSArg(const CallExpr *Call, const Expr *&UnsafeArg,
846905
else
847906
goto CHECK_UNSAFE_PTR;
848907

849-
StringFormatStringHandler Handler(Call, FmtArgIdx, UnsafeArg);
908+
StringFormatStringHandler Handler(Call, FmtArgIdx, UnsafeArg, Ctx);
850909

851910
return analyze_format_string::ParsePrintfString(
852911
Handler, FmtStr.begin(), FmtStr.end(), Ctx.getLangOpts(),

0 commit comments

Comments
 (0)