Skip to content

Commit 037fc58

Browse files
committed
various changes from review
based on review from gh@zwuis and gh@cor3ntin - collapse if into parent if - use make_scope_exit over hand-rolled RAII obj - avoid casting a DeclForInitializer back to VarDecl since it already is one - make a few functions correctly formatted with lowercase - make EmitPossiblyUnreachableDiags static
1 parent 6cb0362 commit 037fc58

File tree

6 files changed

+28
-42
lines changed

6 files changed

+28
-42
lines changed

clang/include/clang/Sema/AnalysisBasedWarnings.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ class AnalysisBasedWarnings {
113113
// Issue warnings that require whole-translation-unit analysis.
114114
void IssueWarnings(TranslationUnitDecl *D);
115115

116-
void RegisterVarDeclWarning(VarDecl *VD, PossiblyUnreachableDiag PUD);
116+
void registerVarDeclWarning(VarDecl *VD, PossiblyUnreachableDiag PUD);
117117

118-
void IssueWarningsForRegisteredVarDecl(VarDecl *VD);
118+
void issueWarningsForRegisteredVarDecl(VarDecl *VD);
119119

120120
// Gets the default policy which is in effect at the given source location.
121121
Policy getPolicyInEffectAt(SourceLocation Loc);
@@ -126,10 +126,6 @@ class AnalysisBasedWarnings {
126126
Policy &getPolicyOverrides() { return PolicyOverrides; }
127127

128128
void PrintStats() const;
129-
130-
template <typename Iterator>
131-
void EmitPossiblyUnreachableDiags(AnalysisDeclContext &AC,
132-
std::pair<Iterator, Iterator> PUDs);
133129
};
134130

135131
} // namespace sema

clang/lib/Parse/ParseInit.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,9 @@ ExprResult Parser::ParseInitializer(Decl *DeclForInitializer) {
590590
// constexpr) should emit runtime warnings.
591591
if (DeclForInitializer && !Actions.ExprEvalContexts.empty()) {
592592
if (auto *VD = dyn_cast<VarDecl>(DeclForInitializer);
593-
VD && VD->isFileVarDecl()) {
594-
if (!VD->getType()->isReferenceType() || VD->isConstexpr()) {
595-
Actions.ExprEvalContexts.back().DeclForInitializer = VD;
596-
}
597-
}
593+
VD && VD->isFileVarDecl() &&
594+
(!VD->getType()->isReferenceType() || VD->isConstexpr()))
595+
Actions.ExprEvalContexts.back().DeclForInitializer = VD;
598596
}
599597

600598
ExprResult init;

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,8 +2735,8 @@ static void flushDiagnostics(Sema &S, const sema::FunctionScopeInfo *fscope) {
27352735
}
27362736

27372737
template <typename Iterator>
2738-
void sema::AnalysisBasedWarnings::EmitPossiblyUnreachableDiags(
2739-
AnalysisDeclContext &AC, std::pair<Iterator, Iterator> PUDs) {
2738+
static void emitPossiblyUnreachableDiags(Sema &S, AnalysisDeclContext &AC,
2739+
std::pair<Iterator, Iterator> PUDs) {
27402740

27412741
if (PUDs.first == PUDs.second)
27422742
return;
@@ -2768,12 +2768,12 @@ void sema::AnalysisBasedWarnings::EmitPossiblyUnreachableDiags(
27682768
}
27692769
}
27702770

2771-
void sema::AnalysisBasedWarnings::RegisterVarDeclWarning(
2771+
void sema::AnalysisBasedWarnings::registerVarDeclWarning(
27722772
VarDecl *VD, clang::sema::PossiblyUnreachableDiag PUD) {
27732773
VarDeclPossiblyUnreachableDiags.emplace(VD, PUD);
27742774
}
27752775

2776-
void sema::AnalysisBasedWarnings::IssueWarningsForRegisteredVarDecl(
2776+
void sema::AnalysisBasedWarnings::issueWarningsForRegisteredVarDecl(
27772777
VarDecl *VD) {
27782778
if (!llvm::is_contained(VarDeclPossiblyUnreachableDiags, VD))
27792779
return;
@@ -2791,8 +2791,8 @@ void sema::AnalysisBasedWarnings::IssueWarningsForRegisteredVarDecl(
27912791
auto Range = VarDeclPossiblyUnreachableDiags.equal_range(VD);
27922792
auto SecondRange =
27932793
llvm::make_second_range(llvm::make_range(Range.first, Range.second));
2794-
EmitPossiblyUnreachableDiags(
2795-
AC, std::make_pair(SecondRange.begin(), SecondRange.end()));
2794+
emitPossiblyUnreachableDiags(
2795+
S, AC, std::make_pair(SecondRange.begin(), SecondRange.end()));
27962796
}
27972797

27982798
// An AST Visitor that calls a callback function on each callable DEFINITION
@@ -3007,7 +3007,7 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
30073007

30083008
// Emit delayed diagnostics.
30093009
auto &PUDs = fscope->PossiblyUnreachableDiags;
3010-
EmitPossiblyUnreachableDiags(AC, std::make_pair(PUDs.begin(), PUDs.end()));
3010+
emitPossiblyUnreachableDiags(S, AC, std::make_pair(PUDs.begin(), PUDs.end()));
30113011

30123012
// Warning: check missing 'return'
30133013
if (P.enableCheckFallThrough) {

clang/lib/Sema/SemaDecl.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "clang/Sema/SemaWasm.h"
6060
#include "clang/Sema/Template.h"
6161
#include "llvm/ADT/STLForwardCompat.h"
62+
#include "llvm/ADT/ScopeExit.h"
6263
#include "llvm/ADT/SmallPtrSet.h"
6364
#include "llvm/ADT/SmallString.h"
6465
#include "llvm/ADT/StringExtras.h"
@@ -13752,15 +13753,10 @@ void Sema::DiagnoseUniqueObjectDuplication(const VarDecl *VD) {
1375213753
}
1375313754

1375413755
void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
13755-
// RAII helper to ensure DeclForInitializer is cleared on all exit paths
13756-
struct ClearDeclForInitializer {
13757-
Sema &S;
13758-
ClearDeclForInitializer(Sema &S) : S(S) {}
13759-
~ClearDeclForInitializer() {
13760-
if (!S.ExprEvalContexts.empty())
13761-
S.ExprEvalContexts.back().DeclForInitializer = nullptr;
13762-
}
13763-
} Clearer(*this);
13756+
auto ResetDeclForInitializer = llvm::make_scope_exit([this]() {
13757+
if (this->ExprEvalContexts.empty())
13758+
this->ExprEvalContexts.back().DeclForInitializer = nullptr;
13759+
});
1376413760

1376513761
// If there is no declaration, there was an error parsing it. Just ignore
1376613762
// the initializer.
@@ -15089,7 +15085,7 @@ void Sema::FinalizeDeclaration(Decl *ThisDecl) {
1508915085

1509015086
// Emit any deferred warnings for the variable's initializer, even if the
1509115087
// variable is invalid
15092-
AnalysisWarnings.IssueWarningsForRegisteredVarDecl(VD);
15088+
AnalysisWarnings.issueWarningsForRegisteredVarDecl(VD);
1509315089

1509415090
// Apply an implicit SectionAttr if '#pragma clang section bss|data|rodata' is active
1509515091
if (VD->hasGlobalStorage() && VD->isThisDeclarationADefinition() &&

clang/lib/Sema/SemaExpr.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20569,16 +20569,15 @@ void Sema::MarkDeclarationsReferencedInExpr(Expr *E,
2056920569
/// Emit a diagnostic when statements are reachable.
2057020570
bool Sema::DiagIfReachable(SourceLocation Loc, ArrayRef<const Stmt *> Stmts,
2057120571
const PartialDiagnostic &PD) {
20572+
VarDecl *Decl = ExprEvalContexts.back().DeclForInitializer;
2057220573
// The initializer of a constexpr variable or of the first declaration of a
2057320574
// static data member is not syntactically a constant evaluated constant,
2057420575
// but nonetheless is always required to be a constant expression, so we
2057520576
// can skip diagnosing.
20576-
if (auto *VD = dyn_cast_or_null<VarDecl>(
20577-
ExprEvalContexts.back().DeclForInitializer)) {
20578-
if (VD->isConstexpr() ||
20579-
(VD->isStaticDataMember() && VD->isFirstDecl() && !VD->isInline()))
20580-
return false;
20581-
}
20577+
if (Decl &&
20578+
(Decl->isConstexpr() || (Decl->isStaticDataMember() &&
20579+
Decl->isFirstDecl() && !Decl->isInline())))
20580+
return false;
2058220581

2058320582
if (Stmts.empty()) {
2058420583
Diag(Loc, PD);
@@ -20594,13 +20593,10 @@ bool Sema::DiagIfReachable(SourceLocation Loc, ArrayRef<const Stmt *> Stmts,
2059420593
// For non-constexpr file-scope variables with reachability context (non-empty
2059520594
// Stmts), build a CFG for the initializer and check whether the context in
2059620595
// question is reachable.
20597-
if (auto *VD = dyn_cast_or_null<VarDecl>(
20598-
ExprEvalContexts.back().DeclForInitializer)) {
20599-
if (VD->isFileVarDecl()) {
20600-
AnalysisWarnings.RegisterVarDeclWarning(
20601-
VD, sema::PossiblyUnreachableDiag(PD, Loc, Stmts));
20602-
return true;
20603-
}
20596+
if (Decl && Decl->isFileVarDecl()) {
20597+
AnalysisWarnings.registerVarDeclWarning(
20598+
Decl, sema::PossiblyUnreachableDiag(PD, Loc, Stmts));
20599+
return true;
2060420600
}
2060520601

2060620602
Diag(Loc, PD);

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6473,7 +6473,7 @@ void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
64736473
Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(),
64746474
OldVar->getPointOfInstantiation());
64756475
// Emit any deferred warnings for the variable's initializer
6476-
AnalysisWarnings.IssueWarningsForRegisteredVarDecl(Var);
6476+
AnalysisWarnings.issueWarningsForRegisteredVarDecl(Var);
64776477
}
64786478

64796479
// This variable may have local implicit instantiations that need to be

0 commit comments

Comments
 (0)