Skip to content

Commit fb35271

Browse files
authored
Merge branch 'main' into bundle-break-phyreg-liveness
2 parents db3dbfc + a9d491b commit fb35271

File tree

80 files changed

+3352
-670
lines changed

Some content is hidden

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

80 files changed

+3352
-670
lines changed

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(),

clang/lib/Basic/Targets/WebAssembly.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
5959
.Case("exception-handling", HasExceptionHandling)
6060
.Case("extended-const", HasExtendedConst)
6161
.Case("fp16", HasFP16)
62-
.Case("gc", HasGC)
6362
.Case("multimemory", HasMultiMemory)
6463
.Case("multivalue", HasMultivalue)
6564
.Case("mutable-globals", HasMutableGlobals)
6665
.Case("nontrapping-fptoint", HasNontrappingFPToInt)
6766
.Case("reference-types", HasReferenceTypes)
67+
.Case("gc", HasGC)
6868
.Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
6969
.Case("sign-ext", HasSignExt)
7070
.Case("simd128", SIMDLevel >= SIMD128)
@@ -99,8 +99,6 @@ void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
9999
Builder.defineMacro("__wasm_multimemory__");
100100
if (HasFP16)
101101
Builder.defineMacro("__wasm_fp16__");
102-
if (HasGC)
103-
Builder.defineMacro("__wasm_gc__");
104102
if (HasMultivalue)
105103
Builder.defineMacro("__wasm_multivalue__");
106104
if (HasMutableGlobals)
@@ -109,6 +107,8 @@ void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
109107
Builder.defineMacro("__wasm_nontrapping_fptoint__");
110108
if (HasReferenceTypes)
111109
Builder.defineMacro("__wasm_reference_types__");
110+
if (HasGC)
111+
Builder.defineMacro("__wasm_gc__");
112112
if (SIMDLevel >= RelaxedSIMD)
113113
Builder.defineMacro("__wasm_relaxed_simd__");
114114
if (HasSignExt)
@@ -194,7 +194,6 @@ bool WebAssemblyTargetInfo::initFeatureMap(
194194
Features["exception-handling"] = true;
195195
Features["extended-const"] = true;
196196
Features["fp16"] = true;
197-
Features["gc"] = true;
198197
Features["multimemory"] = true;
199198
Features["tail-call"] = true;
200199
Features["wide-arithmetic"] = true;
@@ -271,14 +270,6 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
271270
HasFP16 = false;
272271
continue;
273272
}
274-
if (Feature == "+gc") {
275-
HasGC = true;
276-
continue;
277-
}
278-
if (Feature == "-gc") {
279-
HasGC = false;
280-
continue;
281-
}
282273
if (Feature == "+multimemory") {
283274
HasMultiMemory = true;
284275
continue;
@@ -319,6 +310,14 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
319310
HasReferenceTypes = false;
320311
continue;
321312
}
313+
if (Feature == "+gc") {
314+
HasGC = true;
315+
continue;
316+
}
317+
if (Feature == "-gc") {
318+
HasGC = false;
319+
continue;
320+
}
322321
if (Feature == "+relaxed-simd") {
323322
SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
324323
continue;

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
6464
bool HasExceptionHandling = false;
6565
bool HasExtendedConst = false;
6666
bool HasFP16 = false;
67-
bool HasGC = false;
6867
bool HasMultiMemory = false;
6968
bool HasMultivalue = false;
7069
bool HasMutableGlobals = false;
7170
bool HasNontrappingFPToInt = false;
7271
bool HasReferenceTypes = false;
72+
bool HasGC = false;
7373
bool HasSignExt = false;
7474
bool HasTailCall = false;
7575
bool HasWideArithmetic = false;

clang/lib/CodeGen/CGCoroutine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ CodeGenFunction::generateAwaitSuspendWrapper(Twine const &CoroName,
435435
llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
436436

437437
llvm::Function *Fn = llvm::Function::Create(
438-
LTy, llvm::GlobalValue::PrivateLinkage, FuncName, &CGM.getModule());
438+
LTy, llvm::GlobalValue::InternalLinkage, FuncName, &CGM.getModule());
439439

440440
Fn->addParamAttr(0, llvm::Attribute::AttrKind::NonNull);
441441
Fn->addParamAttr(0, llvm::Attribute::AttrKind::NoUndef);

clang/lib/Driver/ToolChains/MinGW.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,18 @@ void tools::MinGW::Linker::AddLibGCC(const ArgList &Args,
8585

8686
CmdArgs.push_back("-lmoldname");
8787
CmdArgs.push_back("-lmingwex");
88-
for (auto Lib : Args.getAllArgValues(options::OPT_l))
88+
for (auto Lib : Args.getAllArgValues(options::OPT_l)) {
8989
if (StringRef(Lib).starts_with("msvcr") ||
9090
StringRef(Lib).starts_with("ucrt") ||
91-
StringRef(Lib).starts_with("crtdll"))
91+
StringRef(Lib).starts_with("crtdll")) {
92+
std::string CRTLib = (llvm::Twine("-l") + Lib).str();
93+
// Respect the user's chosen crt variant, but still provide it
94+
// again as the last linker argument, because some of the libraries
95+
// we added above may depend on it.
96+
CmdArgs.push_back(Args.MakeArgStringRef(CRTLib));
9297
return;
98+
}
99+
}
93100
CmdArgs.push_back("-lmsvcrt");
94101
}
95102

clang/lib/Sema/SemaConcept.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,6 @@ static bool CheckFunctionConstraintsWithoutInstantiation(
11051105
}
11061106

11071107
Sema::ContextRAII SavedContext(SemaRef, FD);
1108-
std::optional<Sema::CXXThisScopeRAII> ThisScope;
1109-
if (auto *Method = dyn_cast<CXXMethodDecl>(FD))
1110-
ThisScope.emplace(SemaRef, /*Record=*/Method->getParent(),
1111-
/*ThisQuals=*/Method->getMethodQualifiers());
11121108
return SemaRef.CheckConstraintSatisfaction(
11131109
Template, TemplateAC, MLTAL, PointOfInstantiation, Satisfaction);
11141110
}

clang/lib/Sema/SemaModule.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "clang/AST/ASTConsumer.h"
1515
#include "clang/AST/ASTMutationListener.h"
16-
#include "clang/AST/RecursiveASTVisitor.h"
16+
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1717
#include "clang/Lex/HeaderSearch.h"
1818
#include "clang/Lex/Preprocessor.h"
1919
#include "clang/Sema/ParsedAttr.h"
@@ -1422,14 +1422,14 @@ bool ExposureChecker::checkExposure(const CXXRecordDecl *RD, bool Diag) {
14221422
return IsExposure;
14231423
}
14241424

1425-
template <typename CallbackTy>
1426-
class ReferenceTULocalChecker
1427-
: public clang::RecursiveASTVisitor<ReferenceTULocalChecker<CallbackTy>> {
1425+
class ReferenceTULocalChecker : public DynamicRecursiveASTVisitor {
14281426
public:
1427+
using CallbackTy = std::function<void(DeclRefExpr *, ValueDecl *)>;
1428+
14291429
ReferenceTULocalChecker(ExposureChecker &C, CallbackTy &&Callback)
14301430
: Checker(C), Callback(std::move(Callback)) {}
14311431

1432-
bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1432+
bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
14331433
ValueDecl *Referenced = DRE->getDecl();
14341434
if (!Referenced)
14351435
return true;
@@ -1468,10 +1468,6 @@ class ReferenceTULocalChecker
14681468
CallbackTy Callback;
14691469
};
14701470

1471-
template <typename CallbackTy>
1472-
ReferenceTULocalChecker(ExposureChecker &, CallbackTy &&)
1473-
-> ReferenceTULocalChecker<CallbackTy>;
1474-
14751471
bool ExposureChecker::checkExposure(const Stmt *S, bool Diag) {
14761472
if (!S)
14771473
return false;

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,8 +4749,6 @@ Sema::CheckConceptTemplateId(const CXXScopeSpec &SS,
47494749
EnterExpressionEvaluationContext EECtx{
47504750
*this, ExpressionEvaluationContext::Unevaluated, CSD};
47514751

4752-
ContextRAII CurContext(*this, CSD->getDeclContext(),
4753-
/*NewThisContext=*/false);
47544752
if (!AreArgsDependent &&
47554753
CheckConstraintSatisfaction(
47564754
NamedConcept, AssociatedConstraint(NamedConcept->getConstraintExpr()),

clang/test/CodeGenCoroutines/coro-await.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extern "C" void f0() {
100100
// CHECK: call i8 @llvm.coro.suspend(token %[[FINALSP_ID]], i1 true)
101101

102102
// Await suspend wrapper
103-
// CHECK: define{{.*}} @f0.__await_suspend_wrapper__await(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
103+
// CHECK: define internal {{.*}} @f0.__await_suspend_wrapper__await(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
104104
// CHECK: store ptr %[[AWAITABLE_ARG]], ptr %[[AWAITABLE_TMP:.+]],
105105
// CHECK: store ptr %[[FRAME_ARG]], ptr %[[FRAME_TMP:.+]],
106106
// CHECK: %[[AWAITABLE:.+]] = load ptr, ptr %[[AWAITABLE_TMP]]
@@ -162,7 +162,7 @@ extern "C" void f1(int) {
162162
// CHECK: call void @_ZN13suspend_maybe12await_resumeEv(ptr {{[^,]*}} %[[AWAITABLE]])
163163

164164
// Await suspend wrapper
165-
// CHECK: define {{.*}} i1 @f1.__await_suspend_wrapper__yield(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
165+
// CHECK: define internal {{.*}} i1 @f1.__await_suspend_wrapper__yield(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
166166
// CHECK: store ptr %[[AWAITABLE_ARG]], ptr %[[AWAITABLE_TMP:.+]],
167167
// CHECK: store ptr %[[FRAME_ARG]], ptr %[[FRAME_TMP:.+]],
168168
// CHECK: %[[AWAITABLE:.+]] = load ptr, ptr %[[AWAITABLE_TMP]]
@@ -379,7 +379,7 @@ extern "C" void TestTailcall() {
379379
// CHECK-NEXT: ]
380380

381381
// Await suspend wrapper
382-
// CHECK: define {{.*}} ptr @TestTailcall.__await_suspend_wrapper__await(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
382+
// CHECK: define internal {{.*}} ptr @TestTailcall.__await_suspend_wrapper__await(ptr {{[^,]*}} %[[AWAITABLE_ARG:.+]], ptr {{[^,]*}} %[[FRAME_ARG:.+]])
383383
// CHECK: store ptr %[[AWAITABLE_ARG]], ptr %[[AWAITABLE_TMP:.+]],
384384
// CHECK: store ptr %[[FRAME_ARG]], ptr %[[FRAME_TMP:.+]],
385385
// CHECK: %[[AWAITABLE:.+]] = load ptr, ptr %[[AWAITABLE_TMP]]

clang/test/Driver/mingw-msvcrt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
// CHECK_DEFAULT: "-lmingwex" "-lmsvcrt" "-ladvapi32"
88
// CHECK_DEFAULT-SAME: "-lmsvcrt" "-lkernel32" "{{.*}}crtend.o"
99
// CHECK_MSVCR120: "-lmsvcr120"
10-
// CHECK_MSVCR120-SAME: "-lmingwex" "-ladvapi32"
10+
// CHECK_MSVCR120-SAME: "-lmingwex" "-lmsvcr120" "-ladvapi32"
1111
// CHECK_UCRTBASE: "-lucrtbase"
12-
// CHECK_UCRTBASE-SAME: "-lmingwex" "-ladvapi32"
12+
// CHECK_UCRTBASE-SAME: "-lmingwex" "-lucrtbase" "-ladvapi32"
1313
// CHECK_UCRT: "-lucrt"
14-
// CHECK_UCRT-SAME: "-lmingwex" "-ladvapi32"
14+
// CHECK_UCRT-SAME: "-lmingwex" "-lucrt" "-ladvapi32"
1515
// CHECK_CRTDLL: "-lcrtdll"
16-
// CHECK_CRTDLL-SAME: "-lmingwex" "-ladvapi32"
16+
// CHECK_CRTDLL-SAME: "-lmingwex" "-lcrtdll" "-ladvapi32"

0 commit comments

Comments
 (0)