Skip to content

Commit 29c0ea9

Browse files
authored
Merge branch 'main' into cuf_char
2 parents ace8001 + 4f2651c commit 29c0ea9

File tree

236 files changed

+5011
-2240
lines changed

Some content is hidden

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

236 files changed

+5011
-2240
lines changed

.github/workflows/libcxx-build-and-test.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ jobs:
202202
matrix:
203203
include:
204204
- config: generic-cxx03
205-
os: macos-latest
205+
os: macos-15
206206
- config: generic-cxx23
207-
os: macos-latest
207+
os: macos-15
208208
- config: generic-modules
209-
os: macos-latest
209+
os: macos-15
210210
- config: apple-configuration
211-
os: macos-latest
211+
os: macos-15
212212
- config: apple-system
213213
os: macos-13
214214
- config: apple-system-hardened

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 45 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ class MatchDescendantVisitor
171171
return VisitorBase::TraverseCXXTypeidExpr(Node);
172172
}
173173

174-
bool TraverseCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
175-
if (!TraverseStmt(Node->getExpr()))
176-
return false;
177-
return VisitorBase::TraverseCXXDefaultInitExpr(Node);
178-
}
179-
180174
bool TraverseStmt(Stmt *Node, DataRecursionQueue *Queue = nullptr) {
181175
if (!Node)
182176
return true;
@@ -1998,18 +1992,14 @@ class DerefSimplePtrArithFixableGadget : public FixableGadget {
19981992
};
19991993

20001994
/// Scan the function and return a list of gadgets found with provided kits.
2001-
static void findGadgets(const Stmt *S, ASTContext &Ctx,
2002-
const UnsafeBufferUsageHandler &Handler,
2003-
bool EmitSuggestions, FixableGadgetList &FixableGadgets,
2004-
WarningGadgetList &WarningGadgets,
2005-
DeclUseTracker &Tracker) {
1995+
static std::tuple<FixableGadgetList, WarningGadgetList, DeclUseTracker>
1996+
findGadgets(const Decl *D, const UnsafeBufferUsageHandler &Handler,
1997+
bool EmitSuggestions) {
20061998

20071999
struct GadgetFinderCallback : MatchFinder::MatchCallback {
2008-
GadgetFinderCallback(FixableGadgetList &FixableGadgets,
2009-
WarningGadgetList &WarningGadgets,
2010-
DeclUseTracker &Tracker)
2011-
: FixableGadgets(FixableGadgets), WarningGadgets(WarningGadgets),
2012-
Tracker(Tracker) {}
2000+
FixableGadgetList FixableGadgets;
2001+
WarningGadgetList WarningGadgets;
2002+
DeclUseTracker Tracker;
20132003

20142004
void run(const MatchFinder::MatchResult &Result) override {
20152005
// In debug mode, assert that we've found exactly one gadget.
@@ -2050,14 +2040,10 @@ static void findGadgets(const Stmt *S, ASTContext &Ctx,
20502040
assert(numFound >= 1 && "Gadgets not found in match result!");
20512041
assert(numFound <= 1 && "Conflicting bind tags in gadgets!");
20522042
}
2053-
2054-
FixableGadgetList &FixableGadgets;
2055-
WarningGadgetList &WarningGadgets;
2056-
DeclUseTracker &Tracker;
20572043
};
20582044

20592045
MatchFinder M;
2060-
GadgetFinderCallback CB{FixableGadgets, WarningGadgets, Tracker};
2046+
GadgetFinderCallback CB;
20612047

20622048
// clang-format off
20632049
M.addMatcher(
@@ -2102,7 +2088,9 @@ static void findGadgets(const Stmt *S, ASTContext &Ctx,
21022088
// clang-format on
21032089
}
21042090

2105-
M.match(*S, Ctx);
2091+
M.match(*D->getBody(), D->getASTContext());
2092+
return {std::move(CB.FixableGadgets), std::move(CB.WarningGadgets),
2093+
std::move(CB.Tracker)};
21062094
}
21072095

21082096
// Compares AST nodes by source locations.
@@ -3646,9 +3634,39 @@ class VariableGroupsManagerImpl : public VariableGroupsManager {
36463634
}
36473635
};
36483636

3649-
void applyGadgets(const Decl *D, FixableGadgetList FixableGadgets,
3650-
WarningGadgetList WarningGadgets, DeclUseTracker Tracker,
3651-
UnsafeBufferUsageHandler &Handler, bool EmitSuggestions) {
3637+
void clang::checkUnsafeBufferUsage(const Decl *D,
3638+
UnsafeBufferUsageHandler &Handler,
3639+
bool EmitSuggestions) {
3640+
#ifndef NDEBUG
3641+
Handler.clearDebugNotes();
3642+
#endif
3643+
3644+
assert(D && D->getBody());
3645+
// We do not want to visit a Lambda expression defined inside a method
3646+
// independently. Instead, it should be visited along with the outer method.
3647+
// FIXME: do we want to do the same thing for `BlockDecl`s?
3648+
if (const auto *fd = dyn_cast<CXXMethodDecl>(D)) {
3649+
if (fd->getParent()->isLambda() && fd->getParent()->isLocalClass())
3650+
return;
3651+
}
3652+
3653+
// Do not emit fixit suggestions for functions declared in an
3654+
// extern "C" block.
3655+
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3656+
for (FunctionDecl *FReDecl : FD->redecls()) {
3657+
if (FReDecl->isExternC()) {
3658+
EmitSuggestions = false;
3659+
break;
3660+
}
3661+
}
3662+
}
3663+
3664+
WarningGadgetSets UnsafeOps;
3665+
FixableGadgetSets FixablesForAllVars;
3666+
3667+
auto [FixableGadgets, WarningGadgets, Tracker] =
3668+
findGadgets(D, Handler, EmitSuggestions);
3669+
36523670
if (!EmitSuggestions) {
36533671
// Our job is very easy without suggestions. Just warn about
36543672
// every problematic operation and consider it done. No need to deal
@@ -3692,10 +3710,8 @@ void applyGadgets(const Decl *D, FixableGadgetList FixableGadgets,
36923710
if (WarningGadgets.empty())
36933711
return;
36943712

3695-
WarningGadgetSets UnsafeOps =
3696-
groupWarningGadgetsByVar(std::move(WarningGadgets));
3697-
FixableGadgetSets FixablesForAllVars =
3698-
groupFixablesByVar(std::move(FixableGadgets));
3713+
UnsafeOps = groupWarningGadgetsByVar(std::move(WarningGadgets));
3714+
FixablesForAllVars = groupFixablesByVar(std::move(FixableGadgets));
36993715

37003716
std::map<const VarDecl *, FixItList> FixItsForVariableGroup;
37013717

@@ -3916,56 +3932,3 @@ void applyGadgets(const Decl *D, FixableGadgetList FixableGadgets,
39163932
}
39173933
}
39183934
}
3919-
3920-
void clang::checkUnsafeBufferUsage(const Decl *D,
3921-
UnsafeBufferUsageHandler &Handler,
3922-
bool EmitSuggestions) {
3923-
#ifndef NDEBUG
3924-
Handler.clearDebugNotes();
3925-
#endif
3926-
3927-
assert(D);
3928-
3929-
SmallVector<Stmt *> Stmts;
3930-
3931-
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3932-
// We do not want to visit a Lambda expression defined inside a method
3933-
// independently. Instead, it should be visited along with the outer method.
3934-
// FIXME: do we want to do the same thing for `BlockDecl`s?
3935-
if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3936-
if (MD->getParent()->isLambda() && MD->getParent()->isLocalClass())
3937-
return;
3938-
}
3939-
3940-
for (FunctionDecl *FReDecl : FD->redecls()) {
3941-
if (FReDecl->isExternC()) {
3942-
// Do not emit fixit suggestions for functions declared in an
3943-
// extern "C" block.
3944-
EmitSuggestions = false;
3945-
break;
3946-
}
3947-
}
3948-
3949-
Stmts.push_back(FD->getBody());
3950-
3951-
if (const auto *ID = dyn_cast<CXXConstructorDecl>(D)) {
3952-
for (const CXXCtorInitializer *CI : ID->inits()) {
3953-
Stmts.push_back(CI->getInit());
3954-
}
3955-
}
3956-
} else if (isa<BlockDecl>(D) || isa<ObjCMethodDecl>(D)) {
3957-
Stmts.push_back(D->getBody());
3958-
}
3959-
3960-
assert(!Stmts.empty());
3961-
3962-
FixableGadgetList FixableGadgets;
3963-
WarningGadgetList WarningGadgets;
3964-
DeclUseTracker Tracker;
3965-
for (Stmt *S : Stmts) {
3966-
findGadgets(S, D->getASTContext(), Handler, EmitSuggestions, FixableGadgets,
3967-
WarningGadgets, Tracker);
3968-
}
3969-
applyGadgets(D, std::move(FixableGadgets), std::move(WarningGadgets),
3970-
std::move(Tracker), Handler, EmitSuggestions);
3971-
}

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,10 +2837,9 @@ llvm::FunctionCallee CodeGenModule::getBlockObjectDispose() {
28372837
if (BlockObjectDispose)
28382838
return BlockObjectDispose;
28392839

2840-
llvm::Type *args[] = { Int8PtrTy, Int32Ty };
2841-
llvm::FunctionType *fty
2842-
= llvm::FunctionType::get(VoidTy, args, false);
2843-
BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
2840+
QualType args[] = {Context.VoidPtrTy, Context.IntTy};
2841+
BlockObjectDispose =
2842+
CreateRuntimeFunction(Context.VoidTy, args, "_Block_object_dispose");
28442843
configureBlocksRuntimeObject(
28452844
*this, cast<llvm::Constant>(BlockObjectDispose.getCallee()));
28462845
return BlockObjectDispose;
@@ -2850,10 +2849,9 @@ llvm::FunctionCallee CodeGenModule::getBlockObjectAssign() {
28502849
if (BlockObjectAssign)
28512850
return BlockObjectAssign;
28522851

2853-
llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
2854-
llvm::FunctionType *fty
2855-
= llvm::FunctionType::get(VoidTy, args, false);
2856-
BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
2852+
QualType args[] = {Context.VoidPtrTy, Context.VoidPtrTy, Context.IntTy};
2853+
BlockObjectAssign =
2854+
CreateRuntimeFunction(Context.VoidTy, args, "_Block_object_assign");
28572855
configureBlocksRuntimeObject(
28582856
*this, cast<llvm::Constant>(BlockObjectAssign.getCallee()));
28592857
return BlockObjectAssign;

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4903,6 +4903,52 @@ GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
49034903
return nullptr;
49044904
}
49054905

4906+
static void setWindowsItaniumDLLImport(CodeGenModule &CGM, bool Local,
4907+
llvm::Function *F, StringRef Name) {
4908+
// In Windows Itanium environments, try to mark runtime functions
4909+
// dllimport. For Mingw and MSVC, don't. We don't really know if the user
4910+
// will link their standard library statically or dynamically. Marking
4911+
// functions imported when they are not imported can cause linker errors
4912+
// and warnings.
4913+
if (!Local && CGM.getTriple().isWindowsItaniumEnvironment() &&
4914+
!CGM.getCodeGenOpts().LTOVisibilityPublicStd) {
4915+
const FunctionDecl *FD = GetRuntimeFunctionDecl(CGM.getContext(), Name);
4916+
if (!FD || FD->hasAttr<DLLImportAttr>()) {
4917+
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4918+
F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4919+
}
4920+
}
4921+
}
4922+
4923+
llvm::FunctionCallee CodeGenModule::CreateRuntimeFunction(
4924+
QualType ReturnTy, ArrayRef<QualType> ArgTys, StringRef Name,
4925+
llvm::AttributeList ExtraAttrs, bool Local, bool AssumeConvergent) {
4926+
if (AssumeConvergent) {
4927+
ExtraAttrs =
4928+
ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
4929+
}
4930+
4931+
QualType FTy = Context.getFunctionType(ReturnTy, ArgTys,
4932+
FunctionProtoType::ExtProtoInfo());
4933+
const CGFunctionInfo &Info = getTypes().arrangeFreeFunctionType(
4934+
Context.getCanonicalType(FTy).castAs<FunctionProtoType>());
4935+
auto *ConvTy = getTypes().GetFunctionType(Info);
4936+
llvm::Constant *C = GetOrCreateLLVMFunction(
4937+
Name, ConvTy, GlobalDecl(), /*ForVTable=*/false,
4938+
/*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
4939+
4940+
if (auto *F = dyn_cast<llvm::Function>(C)) {
4941+
if (F->empty()) {
4942+
SetLLVMFunctionAttributes(GlobalDecl(), Info, F, /*IsThunk*/ false);
4943+
// FIXME: Set calling-conv properly in ExtProtoInfo
4944+
F->setCallingConv(getRuntimeCC());
4945+
setWindowsItaniumDLLImport(*this, Local, F, Name);
4946+
setDSOLocal(F);
4947+
}
4948+
}
4949+
return {ConvTy, C};
4950+
}
4951+
49064952
/// CreateRuntimeFunction - Create a new runtime function with the specified
49074953
/// type and name.
49084954
llvm::FunctionCallee
@@ -4922,24 +4968,12 @@ CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
49224968
if (auto *F = dyn_cast<llvm::Function>(C)) {
49234969
if (F->empty()) {
49244970
F->setCallingConv(getRuntimeCC());
4925-
4926-
// In Windows Itanium environments, try to mark runtime functions
4927-
// dllimport. For Mingw and MSVC, don't. We don't really know if the user
4928-
// will link their standard library statically or dynamically. Marking
4929-
// functions imported when they are not imported can cause linker errors
4930-
// and warnings.
4931-
if (!Local && getTriple().isWindowsItaniumEnvironment() &&
4932-
!getCodeGenOpts().LTOVisibilityPublicStd) {
4933-
const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
4934-
if (!FD || FD->hasAttr<DLLImportAttr>()) {
4935-
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4936-
F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4937-
}
4938-
}
4971+
setWindowsItaniumDLLImport(*this, Local, F, Name);
49394972
setDSOLocal(F);
49404973
// FIXME: We should use CodeGenModule::SetLLVMFunctionAttributes() instead
49414974
// of trying to approximate the attributes using the LLVM function
4942-
// signature. This requires revising the API of CreateRuntimeFunction().
4975+
// signature. The other overload of CreateRuntimeFunction does this; it
4976+
// should be used for new code.
49434977
markRegisterParameterAttributes(F);
49444978
}
49454979
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,23 @@ class CodeGenModule : public CodeGenTypeCache {
12471247
/// Create or return a runtime function declaration with the specified type
12481248
/// and name. If \p AssumeConvergent is true, the call will have the
12491249
/// convergent attribute added.
1250+
///
1251+
/// For new code, please use the overload that takes a QualType; it sets
1252+
/// function attributes more accurately.
12501253
llvm::FunctionCallee
12511254
CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name,
12521255
llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
12531256
bool Local = false, bool AssumeConvergent = false);
12541257

1258+
/// Create or return a runtime function declaration with the specified type
1259+
/// and name. If \p AssumeConvergent is true, the call will have the
1260+
/// convergent attribute added.
1261+
llvm::FunctionCallee
1262+
CreateRuntimeFunction(QualType ReturnTy, ArrayRef<QualType> ArgTys,
1263+
StringRef Name,
1264+
llvm::AttributeList ExtraAttrs = llvm::AttributeList(),
1265+
bool Local = false, bool AssumeConvergent = false);
1266+
12551267
/// Create a new runtime global variable with the specified type and name.
12561268
llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
12571269
StringRef Name);

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ static bool isSignedCharDefault(const llvm::Triple &Triple) {
13411341
return false;
13421342

13431343
case llvm::Triple::hexagon:
1344+
case llvm::Triple::msp430:
13441345
case llvm::Triple::ppcle:
13451346
case llvm::Triple::ppc64le:
13461347
case llvm::Triple::riscv32:

0 commit comments

Comments
 (0)