Skip to content

Commit 808bf02

Browse files
authored
merge main into amd-staging (llvm#691)
2 parents 09ac879 + 5cb25dd commit 808bf02

File tree

345 files changed

+4862
-1517
lines changed

Some content is hidden

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

345 files changed

+4862
-1517
lines changed

clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ static bool checkOverrideByDerivedMethod(const CXXMethodDecl *BaseMD,
179179

180180
bool VirtualNearMissCheck::isPossibleToBeOverridden(
181181
const CXXMethodDecl *BaseMD) {
182-
auto Iter = PossibleMap.find(BaseMD);
183-
if (Iter != PossibleMap.end())
182+
auto [Iter, Inserted] = PossibleMap.try_emplace(BaseMD);
183+
if (!Inserted)
184184
return Iter->second;
185185

186186
bool IsPossible = !BaseMD->isImplicit() && !isa<CXXConstructorDecl>(BaseMD) &&
187187
!isa<CXXDestructorDecl>(BaseMD) && BaseMD->isVirtual() &&
188188
!BaseMD->isOverloadedOperator() &&
189189
!isa<CXXConversionDecl>(BaseMD);
190-
PossibleMap[BaseMD] = IsPossible;
190+
Iter->second = IsPossible;
191191
return IsPossible;
192192
}
193193

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ Code Completion
270270
Static Analyzer
271271
---------------
272272

273+
- Clang currently support extending lifetime of object bound to
274+
reference members of aggregates in CFG and ExprEngine, that are
275+
created from default member initializer.
276+
273277
New features
274278
^^^^^^^^^^^^
275279

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,6 @@ class ASTWriter : public ASTDeserializationListener,
751751
/// Get the unique number used to refer to the given macro.
752752
serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
753753

754-
/// Determine the ID of an already-emitted macro.
755-
serialization::MacroID getMacroID(MacroInfo *MI);
756-
757754
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
758755

759756
/// Emit a reference to a type.

clang/lib/AST/ExternalASTMerger.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,14 @@ class LazyASTImporter : public ASTImporter {
206206
<< "\n";
207207
Source<DeclContext *> FromDC(
208208
cast<DeclContext>(From)->getPrimaryContext());
209-
if (FromOrigins.count(FromDC) &&
210-
Parent.HasImporterForOrigin(*FromOrigins.at(FromDC).AST)) {
209+
if (auto It = FromOrigins.find(FromDC);
210+
It != FromOrigins.end() &&
211+
Parent.HasImporterForOrigin(*It->second.AST)) {
211212
if (LoggingEnabled)
212-
logs() << "(ExternalASTMerger*)" << (void*)&Parent
213-
<< " forced origin (DeclContext*)"
214-
<< (void*)FromOrigins.at(FromDC).DC
215-
<< ", (ASTContext*)"
216-
<< (void*)FromOrigins.at(FromDC).AST
217-
<< "\n";
218-
Parent.ForceRecordOrigin(ToDC, FromOrigins.at(FromDC));
213+
logs() << "(ExternalASTMerger*)" << (void *)&Parent
214+
<< " forced origin (DeclContext*)" << (void *)It->second.DC
215+
<< ", (ASTContext*)" << (void *)It->second.AST << "\n";
216+
Parent.ForceRecordOrigin(ToDC, It->second);
219217
} else {
220218
if (LoggingEnabled)
221219
logs() << "(ExternalASTMerger*)" << (void*)&Parent

clang/lib/AST/ParentMap.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/AST/ParentMap.h"
1414
#include "clang/AST/Decl.h"
1515
#include "clang/AST/Expr.h"
16+
#include "clang/AST/ExprCXX.h"
1617
#include "clang/AST/StmtObjC.h"
1718
#include "llvm/ADT/DenseMap.h"
1819

@@ -103,6 +104,22 @@ static void BuildParentMap(MapTy& M, Stmt* S,
103104
BuildParentMap(M, SubStmt, OVMode);
104105
}
105106
break;
107+
case Stmt::CXXDefaultArgExprClass:
108+
if (auto *Arg = dyn_cast<CXXDefaultArgExpr>(S)) {
109+
if (Arg->hasRewrittenInit()) {
110+
M[Arg->getExpr()] = S;
111+
BuildParentMap(M, Arg->getExpr(), OVMode);
112+
}
113+
}
114+
break;
115+
case Stmt::CXXDefaultInitExprClass:
116+
if (auto *Init = dyn_cast<CXXDefaultInitExpr>(S)) {
117+
if (Init->hasRewrittenInit()) {
118+
M[Init->getExpr()] = S;
119+
BuildParentMap(M, Init->getExpr(), OVMode);
120+
}
121+
}
122+
break;
106123
default:
107124
for (Stmt *SubStmt : S->children()) {
108125
if (SubStmt) {

clang/lib/Analysis/CFG.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ class CFGBuilder {
556556

557557
private:
558558
// Visitors to walk an AST and construct the CFG.
559+
CFGBlock *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Default,
560+
AddStmtChoice asc);
561+
CFGBlock *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Default,
562+
AddStmtChoice asc);
559563
CFGBlock *VisitInitListExpr(InitListExpr *ILE, AddStmtChoice asc);
560564
CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
561565
CFGBlock *VisitAttributedStmt(AttributedStmt *A, AddStmtChoice asc);
@@ -2263,16 +2267,10 @@ CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc,
22632267
asc, ExternallyDestructed);
22642268

22652269
case Stmt::CXXDefaultArgExprClass:
2270+
return VisitCXXDefaultArgExpr(cast<CXXDefaultArgExpr>(S), asc);
2271+
22662272
case Stmt::CXXDefaultInitExprClass:
2267-
// FIXME: The expression inside a CXXDefaultArgExpr is owned by the
2268-
// called function's declaration, not by the caller. If we simply add
2269-
// this expression to the CFG, we could end up with the same Expr
2270-
// appearing multiple times (PR13385).
2271-
//
2272-
// It's likewise possible for multiple CXXDefaultInitExprs for the same
2273-
// expression to be used in the same function (through aggregate
2274-
// initialization).
2275-
return VisitStmt(S, asc);
2273+
return VisitCXXDefaultInitExpr(cast<CXXDefaultInitExpr>(S), asc);
22762274

22772275
case Stmt::CXXBindTemporaryExprClass:
22782276
return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
@@ -2442,6 +2440,44 @@ CFGBlock *CFGBuilder::VisitChildren(Stmt *S) {
24422440
return B;
24432441
}
24442442

2443+
CFGBlock *CFGBuilder::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Arg,
2444+
AddStmtChoice asc) {
2445+
if (Arg->hasRewrittenInit()) {
2446+
if (asc.alwaysAdd(*this, Arg)) {
2447+
autoCreateBlock();
2448+
appendStmt(Block, Arg);
2449+
}
2450+
return VisitStmt(Arg->getExpr()->IgnoreParens(), asc);
2451+
}
2452+
2453+
// We can't add the default argument if it's not rewritten because the
2454+
// expression inside a CXXDefaultArgExpr is owned by the called function's
2455+
// declaration, not by the caller, we could end up with the same expression
2456+
// appearing multiple times.
2457+
return VisitStmt(Arg, asc);
2458+
}
2459+
2460+
CFGBlock *CFGBuilder::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Init,
2461+
AddStmtChoice asc) {
2462+
if (Init->hasRewrittenInit()) {
2463+
if (asc.alwaysAdd(*this, Init)) {
2464+
autoCreateBlock();
2465+
appendStmt(Block, Init);
2466+
}
2467+
2468+
// Unlike CXXDefaultArgExpr::getExpr stripped off the top level FullExpr and
2469+
// ConstantExpr, CXXDefaultInitExpr::getExpr does not do this, so we don't
2470+
// need to ignore ParenExprs, because the top level will not be a ParenExpr.
2471+
return VisitStmt(Init->getExpr(), asc);
2472+
}
2473+
2474+
// We can't add the default initializer if it's not rewritten because multiple
2475+
// CXXDefaultInitExprs for the same sub-expression to be used in the same
2476+
// function (through aggregate initialization). we could end up with the same
2477+
// expression appearing multiple times.
2478+
return VisitStmt(Init, asc);
2479+
}
2480+
24452481
CFGBlock *CFGBuilder::VisitInitListExpr(InitListExpr *ILE, AddStmtChoice asc) {
24462482
if (asc.alwaysAdd(*this, ILE)) {
24472483
autoCreateBlock();

clang/lib/Analysis/ReachableCode.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,12 @@ bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
454454
return isDeadRoot;
455455
}
456456

457-
// Check if the given `DeadStmt` is a coroutine statement and is a substmt of
458-
// the coroutine statement. `Block` is the CFGBlock containing the `DeadStmt`.
459-
static bool isInCoroutineStmt(const Stmt *DeadStmt, const CFGBlock *Block) {
457+
// Check if the given `DeadStmt` is one of target statements or is a sub-stmt of
458+
// them. `Block` is the CFGBlock containing the `DeadStmt`.
459+
template <class... Ts>
460+
static bool isDeadStmtInOneOf(const Stmt *DeadStmt, const CFGBlock *Block) {
460461
// The coroutine statement, co_return, co_await, or co_yield.
461-
const Stmt *CoroStmt = nullptr;
462+
const Stmt *TargetStmt = nullptr;
462463
// Find the first coroutine statement after the DeadStmt in the block.
463464
bool AfterDeadStmt = false;
464465
for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I != E;
@@ -467,32 +468,27 @@ static bool isInCoroutineStmt(const Stmt *DeadStmt, const CFGBlock *Block) {
467468
const Stmt *S = CS->getStmt();
468469
if (S == DeadStmt)
469470
AfterDeadStmt = true;
470-
if (AfterDeadStmt &&
471-
// For simplicity, we only check simple coroutine statements.
472-
(llvm::isa<CoreturnStmt>(S) || llvm::isa<CoroutineSuspendExpr>(S))) {
473-
CoroStmt = S;
471+
if (AfterDeadStmt && llvm::isa<Ts...>(S)) {
472+
TargetStmt = S;
474473
break;
475474
}
476475
}
477-
if (!CoroStmt)
476+
if (!TargetStmt)
478477
return false;
479478
struct Checker : DynamicRecursiveASTVisitor {
480479
const Stmt *DeadStmt;
481-
bool CoroutineSubStmt = false;
482-
Checker(const Stmt *S) : DeadStmt(S) {
483-
// Statements captured in the CFG can be implicit.
484-
ShouldVisitImplicitCode = true;
485-
}
480+
bool IsSubStmtOfTargetStmt = false;
481+
Checker(const Stmt *S) : DeadStmt(S) { ShouldVisitImplicitCode = true; }
486482

487483
bool VisitStmt(Stmt *S) override {
488484
if (S == DeadStmt)
489-
CoroutineSubStmt = true;
485+
IsSubStmtOfTargetStmt = true;
490486
return true;
491487
}
492488
};
493489
Checker checker(DeadStmt);
494-
checker.TraverseStmt(const_cast<Stmt *>(CoroStmt));
495-
return checker.CoroutineSubStmt;
490+
checker.TraverseStmt(const_cast<Stmt *>(TargetStmt));
491+
return checker.IsSubStmtOfTargetStmt;
496492
}
497493

498494
static bool isValidDeadStmt(const Stmt *S, const clang::CFGBlock *Block) {
@@ -503,7 +499,12 @@ static bool isValidDeadStmt(const Stmt *S, const clang::CFGBlock *Block) {
503499
// Coroutine statements are never considered dead statements, because removing
504500
// them may change the function semantic if it is the only coroutine statement
505501
// of the coroutine.
506-
return !isInCoroutineStmt(S, Block);
502+
//
503+
// If the dead stmt is a sub-stmt of CXXDefaultInitExpr and CXXDefaultArgExpr,
504+
// we would rather expect to find CXXDefaultInitExpr and CXXDefaultArgExpr as
505+
// a valid dead stmt.
506+
return !isDeadStmtInOneOf<CoreturnStmt, CoroutineSuspendExpr,
507+
CXXDefaultArgExpr, CXXDefaultInitExpr>(S, Block);
507508
}
508509

509510
const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,13 @@ void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
452452

453453
void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
454454
ArgStringList &CmdArgs) const {
455+
Generic_ELF::AddCXXStdlibLibArgs(Args, CmdArgs);
455456
unsigned Major = getTriple().getOSMajorVersion();
456-
bool Profiling = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
457-
458-
CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
459-
if (Args.hasArg(options::OPT_fexperimental_library))
460-
CmdArgs.push_back("-lc++experimental");
457+
bool SuffixedLib = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
458+
if (SuffixedLib && GetCXXStdlibType(Args) == CST_Libcxx)
459+
std::replace_if(
460+
CmdArgs.begin(), CmdArgs.end(),
461+
[](const char *S) { return StringRef(S) == "-lc++"; }, "-lc++_p");
461462
}
462463

463464
void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
510510
break;
511511
do {
512512
NextTok = Tokens->getNextToken();
513-
} while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
513+
} while (!NextTok->HasUnescapedNewline && NextTok->isNot(tok::eof));
514514

515515
while (NextTok->is(tok::comment))
516516
NextTok = Tokens->getNextToken();

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5570,8 +5570,10 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc,
55705570
/*SkipImmediateInvocations=*/NestedDefaultChecking))
55715571
return ExprError();
55725572

5573+
Expr *RewrittenExpr = (Init == Param->getDefaultArg() ? nullptr : Init);
55735574
return CXXDefaultArgExpr::Create(Context, InitializationContext->Loc, Param,
5574-
Init, InitializationContext->Context);
5575+
RewrittenExpr,
5576+
InitializationContext->Context);
55755577
}
55765578

55775579
static FieldDecl *FindFieldDeclInstantiationPattern(const ASTContext &Ctx,
@@ -5689,10 +5691,11 @@ ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) {
56895691
return ExprError();
56905692
}
56915693
Init = Res.get();
5692-
5694+
Expr *RewrittenInit =
5695+
(Init == Field->getInClassInitializer() ? nullptr : Init);
56935696
return CXXDefaultInitExpr::Create(Context, InitializationContext->Loc,
56945697
Field, InitializationContext->Context,
5695-
Init);
5698+
RewrittenInit);
56965699
}
56975700

56985701
// DR1351:

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13672,7 +13672,7 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
1367213672
Desig.AddDesignator(
1367313673
Designator::CreateArrayDesignator(Index.get(), D.getLBracketLoc()));
1367413674

13675-
ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(D);
13675+
ExprChanged = ExprChanged || Index.get() != E->getArrayIndex(D);
1367613676
ArrayExprs.push_back(Index.get());
1367713677
continue;
1367813678
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6666,14 +6666,6 @@ MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
66666666
return ID;
66676667
}
66686668

6669-
MacroID ASTWriter::getMacroID(MacroInfo *MI) {
6670-
if (!MI || MI->isBuiltinMacro())
6671-
return 0;
6672-
6673-
assert(MacroIDs.contains(MI) && "Macro not emitted!");
6674-
return MacroIDs[MI];
6675-
}
6676-
66776669
uint32_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
66786670
return IdentMacroDirectivesOffsetMap.lookup(Name);
66796671
}

clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,13 @@ class FindStackRegionsSymbolVisitor final : public SymbolVisitor {
274274
void SaveIfEscapes(const MemRegion *MR) {
275275
const StackSpaceRegion *SSR =
276276
MR->getMemorySpace()->getAs<StackSpaceRegion>();
277-
if (SSR && SSR->getStackFrame() == PoppedStackFrame)
277+
278+
if (!SSR)
279+
return;
280+
281+
const StackFrameContext *CapturedSFC = SSR->getStackFrame();
282+
if (CapturedSFC == PoppedStackFrame ||
283+
PoppedStackFrame->isParentOf(CapturedSFC))
278284
EscapingStackRegions.push_back(MR);
279285
}
280286

0 commit comments

Comments
 (0)