Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit d9b4f9f

Browse files
committed
Factor out function to determine whether we're performing a template
instantiation. In preparation for converting the template stack to a more general context stack (so we can include context notes for other kinds of context). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295686 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 644e38e commit d9b4f9f

14 files changed

+55
-48
lines changed

include/clang/Sema/Sema.h

+14
Original file line numberDiff line numberDiff line change
@@ -7182,6 +7182,20 @@ class Sema {
71827182
operator=(const InstantiatingTemplate&) = delete;
71837183
};
71847184

7185+
/// Determine whether we are currently performing template instantiation.
7186+
bool inTemplateInstantiation() const {
7187+
return ActiveTemplateInstantiations.size() > NonInstantiationEntries;
7188+
}
7189+
7190+
void PrintContextStack() {
7191+
if (!ActiveTemplateInstantiations.empty() &&
7192+
ActiveTemplateInstantiations.back() !=
7193+
LastTemplateInstantiationErrorContext) {
7194+
PrintInstantiationStack();
7195+
LastTemplateInstantiationErrorContext =
7196+
ActiveTemplateInstantiations.back();
7197+
}
7198+
}
71857199
void PrintInstantiationStack();
71867200

71877201
/// \brief Determines whether we are currently in a context where

lib/Sema/Sema.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ bool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
327327
if (!fn) return false;
328328

329329
// If we're in template instantiation, it's an error.
330-
if (!ActiveTemplateInstantiations.empty())
330+
if (inTemplateInstantiation())
331331
return false;
332332

333333
// If that function's not in a system header, it's an error.
@@ -1006,7 +1006,7 @@ void Sema::EmitCurrentDiagnostic(unsigned DiagID) {
10061006
// and yet we also use the current diag ID on the DiagnosticsEngine. This has
10071007
// been made more painfully obvious by the refactor that introduced this
10081008
// function, but it is possible that the incoming argument can be
1009-
// eliminnated. If it truly cannot be (for example, there is some reentrancy
1009+
// eliminated. If it truly cannot be (for example, there is some reentrancy
10101010
// issue I am not seeing yet), then there should at least be a clarifying
10111011
// comment somewhere.
10121012
if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) {
@@ -1094,13 +1094,8 @@ void Sema::EmitCurrentDiagnostic(unsigned DiagID) {
10941094
// that is different from the last template instantiation where
10951095
// we emitted an error, print a template instantiation
10961096
// backtrace.
1097-
if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
1098-
!ActiveTemplateInstantiations.empty() &&
1099-
ActiveTemplateInstantiations.back()
1100-
!= LastTemplateInstantiationErrorContext) {
1101-
PrintInstantiationStack();
1102-
LastTemplateInstantiationErrorContext = ActiveTemplateInstantiations.back();
1103-
}
1097+
if (!DiagnosticIDs::isBuiltinNote(DiagID))
1098+
PrintContextStack();
11041099
}
11051100

11061101
Sema::SemaDiagnosticBuilder

lib/Sema/SemaChecking.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
244244
// Scopes aren't available during instantiation. Fortunately, builtin
245245
// functions cannot be template args so they cannot be formed through template
246246
// instantiation. Therefore checking once during the parse is sufficient.
247-
if (!SemaRef.ActiveTemplateInstantiations.empty())
247+
if (SemaRef.inTemplateInstantiation())
248248
return false;
249249

250250
Scope *S = SemaRef.getCurScope();
@@ -6782,7 +6782,7 @@ void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
67826782
if (!Call || !FDecl) return;
67836783

67846784
// Ignore template specializations and macros.
6785-
if (!ActiveTemplateInstantiations.empty()) return;
6785+
if (inTemplateInstantiation()) return;
67866786
if (Call->getExprLoc().isMacroID()) return;
67876787

67886788
// Only care about the one template argument, two function parameter std::max
@@ -8235,7 +8235,7 @@ bool HasEnumType(Expr *E) {
82358235

82368236
void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
82378237
// Disable warning in template instantiations.
8238-
if (!S.ActiveTemplateInstantiations.empty())
8238+
if (S.inTemplateInstantiation())
82398239
return;
82408240

82418241
BinaryOperatorKind op = E->getOpcode();
@@ -8265,7 +8265,7 @@ void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, Expr *Constant,
82658265
Expr *Other, const llvm::APSInt &Value,
82668266
bool RhsConstant) {
82678267
// Disable warning in template instantiations.
8268-
if (!S.ActiveTemplateInstantiations.empty())
8268+
if (S.inTemplateInstantiation())
82698269
return;
82708270

82718271
// TODO: Investigate using GetExprRange() to get tighter bounds
@@ -8703,7 +8703,7 @@ void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
87038703

87048704
SourceLocation CContext) {
87058705
const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
8706-
const bool PruneWarnings = !S.ActiveTemplateInstantiations.empty();
8706+
const bool PruneWarnings = S.inTemplateInstantiation();
87078707

87088708
Expr *InnerE = E->IgnoreParenImpCasts();
87098709
// We also want to warn on, e.g., "int i = -1.234"
@@ -11314,7 +11314,7 @@ void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
1131411314
if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
1131511315
return;
1131611316

11317-
if (!ActiveTemplateInstantiations.empty())
11317+
if (inTemplateInstantiation())
1131811318
return;
1131911319

1132011320
// Strip parens and casts away.

lib/Sema/SemaDecl.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -3088,7 +3088,7 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD,
30883088
// [...] A member shall not be declared twice in the
30893089
// member-specification, except that a nested class or member
30903090
// class template can be declared and then later defined.
3091-
if (ActiveTemplateInstantiations.empty()) {
3091+
if (!inTemplateInstantiation()) {
30923092
unsigned NewDiag;
30933093
if (isa<CXXConstructorDecl>(OldMethod))
30943094
NewDiag = diag::err_constructor_redeclared;
@@ -9061,7 +9061,7 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
90619061

90629062
// Warn that we did this, if we're not performing template instantiation.
90639063
// In that case, we'll have warned already when the template was defined.
9064-
if (ActiveTemplateInstantiations.empty()) {
9064+
if (!inTemplateInstantiation()) {
90659065
SourceLocation AddConstLoc;
90669066
if (FunctionTypeLoc FTL = MD->getTypeSourceInfo()->getTypeLoc()
90679067
.IgnoreParens().getAs<FunctionTypeLoc>())
@@ -9918,8 +9918,8 @@ QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl,
99189918
// checks.
99199919
// We only want to warn outside of template instantiations, though:
99209920
// inside a template, the 'id' could have come from a parameter.
9921-
if (ActiveTemplateInstantiations.empty() && !DefaultedAnyToId &&
9922-
!IsInitCapture && !DeducedType.isNull() && DeducedType->isObjCIdType()) {
9921+
if (!inTemplateInstantiation() && !DefaultedAnyToId && !IsInitCapture &&
9922+
!DeducedType.isNull() && DeducedType->isObjCIdType()) {
99239923
SourceLocation Loc = TSI->getTypeLoc().getBeginLoc();
99249924
Diag(Loc, diag::warn_auto_var_is_id) << VN << Range;
99259925
}
@@ -10821,7 +10821,7 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
1082110821
// Apply section attributes and pragmas to global variables.
1082210822
bool GlobalStorage = var->hasGlobalStorage();
1082310823
if (GlobalStorage && var->isThisDeclarationADefinition() &&
10824-
ActiveTemplateInstantiations.empty()) {
10824+
!inTemplateInstantiation()) {
1082510825
PragmaStack<StringLiteral *> *Stack = nullptr;
1082610826
int SectionFlags = ASTContext::PSF_Implicit | ASTContext::PSF_Read;
1082710827
if (var->getType().isConstQualified())
@@ -11472,7 +11472,7 @@ ParmVarDecl *Sema::BuildParmVarDeclForTypedef(DeclContext *DC,
1147211472
void Sema::DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters) {
1147311473
// Don't diagnose unused-parameter errors in template instantiations; we
1147411474
// will already have done so in the template itself.
11475-
if (!ActiveTemplateInstantiations.empty())
11475+
if (inTemplateInstantiation())
1147611476
return;
1147711477

1147811478
for (const ParmVarDecl *Parameter : Parameters) {
@@ -11822,14 +11822,14 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
1182211822
// captures during transformation of nested lambdas, it is necessary to
1182311823
// have the LSI properly restored.
1182411824
if (isGenericLambdaCallOperatorSpecialization(FD)) {
11825-
assert(ActiveTemplateInstantiations.size() &&
11826-
"There should be an active template instantiation on the stack "
11827-
"when instantiating a generic lambda!");
11825+
assert(inTemplateInstantiation() &&
11826+
"There should be an active template instantiation on the stack "
11827+
"when instantiating a generic lambda!");
1182811828
RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this);
11829-
}
11830-
else
11829+
} else {
1183111830
// Enter a new function scope
1183211831
PushFunctionScope();
11832+
}
1183311833

1183411834
// Builtin functions cannot be defined.
1183511835
if (unsigned BuiltinID = FD->getBuiltinID()) {
@@ -12706,7 +12706,7 @@ bool Sema::isAcceptableTagRedeclaration(const TagDecl *Previous,
1270612706
if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Previous))
1270712707
isTemplate = Record->getDescribedClassTemplate();
1270812708

12709-
if (!ActiveTemplateInstantiations.empty()) {
12709+
if (inTemplateInstantiation()) {
1271012710
// In a template instantiation, do not offer fix-its for tag mismatches
1271112711
// since they usually mess up the template instead of fixing the problem.
1271212712
Diag(NewTagLoc, diag::warn_struct_class_tag_mismatch)

lib/Sema/SemaDeclAttr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
14201420
// check if the attribute came from a macro expansion or a template
14211421
// instantiation.
14221422
if (NonNullArgs.empty() && Attr.getLoc().isFileID() &&
1423-
S.ActiveTemplateInstantiations.empty()) {
1423+
!S.inTemplateInstantiation()) {
14241424
bool AnyPointers = isFunctionOrMethodVariadic(D);
14251425
for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
14261426
I != E && !AnyPointers; ++I) {

lib/Sema/SemaDeclCXX.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12970,7 +12970,7 @@ checkLiteralOperatorTemplateParameterList(Sema &SemaRef,
1297012970
PmArgs->getType()->getAs<TemplateTypeParmType>();
1297112971
if (TArgs && TArgs->getDepth() == PmType->getDepth() &&
1297212972
TArgs->getIndex() == PmType->getIndex()) {
12973-
if (SemaRef.ActiveTemplateInstantiations.empty())
12973+
if (!SemaRef.inTemplateInstantiation())
1297412974
SemaRef.Diag(TpDecl->getLocation(),
1297512975
diag::ext_string_literal_operator_template);
1297612976
return false;

lib/Sema/SemaExpr.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc,
14411441

14421442
// The controlling expression is an unevaluated operand, so side effects are
14431443
// likely unintended.
1444-
if (ActiveTemplateInstantiations.empty() &&
1444+
if (!inTemplateInstantiation() &&
14451445
ControllingExpr->HasSideEffects(Context, false))
14461446
Diag(ControllingExpr->getExprLoc(),
14471447
diag::warn_side_effects_unevaluated_context);
@@ -3695,7 +3695,7 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E,
36953695
// The operand for sizeof and alignof is in an unevaluated expression context,
36963696
// so side effects could result in unintended consequences.
36973697
if ((ExprKind == UETT_SizeOf || ExprKind == UETT_AlignOf) &&
3698-
ActiveTemplateInstantiations.empty() && E->HasSideEffects(Context, false))
3698+
!inTemplateInstantiation() && E->HasSideEffects(Context, false))
36993699
Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context);
37003700

37013701
if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(),
@@ -9259,7 +9259,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
92599259
!(LHSType->isBlockPointerType() && IsRelational) &&
92609260
!LHS.get()->getLocStart().isMacroID() &&
92619261
!RHS.get()->getLocStart().isMacroID() &&
9262-
ActiveTemplateInstantiations.empty()) {
9262+
!inTemplateInstantiation()) {
92639263
// For non-floating point types, check for self-comparisons of the form
92649264
// x == x, x != x, x < x, etc. These always evaluate to a constant, and
92659265
// often indicate logic errors in the program.
@@ -9731,8 +9731,7 @@ QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS,
97319731
// For non-floating point types, check for self-comparisons of the form
97329732
// x == x, x != x, x < x, etc. These always evaluate to a constant, and
97339733
// often indicate logic errors in the program.
9734-
if (!LHSType->hasFloatingRepresentation() &&
9735-
ActiveTemplateInstantiations.empty()) {
9734+
if (!LHSType->hasFloatingRepresentation() && !inTemplateInstantiation()) {
97369735
if (DeclRefExpr* DRL
97379736
= dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts()))
97389737
if (DeclRefExpr* DRR
@@ -9820,7 +9819,7 @@ inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS,
98209819
!LHS.get()->getType()->isBooleanType() &&
98219820
RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() &&
98229821
// Don't warn in macros or template instantiations.
9823-
!Loc.isMacroID() && ActiveTemplateInstantiations.empty()) {
9822+
!Loc.isMacroID() && !inTemplateInstantiation()) {
98249823
// If the RHS can be constant folded, and if it constant folds to something
98259824
// that isn't 0 or 1 (which indicate a potential logical operation that
98269825
// happened to fold to true/false) then warn.
@@ -10356,7 +10355,7 @@ void Sema::DiagnoseCommaOperator(const Expr *LHS, SourceLocation Loc) {
1035610355
return;
1035710356

1035810357
// Don't warn in template instantiations.
10359-
if (!ActiveTemplateInstantiations.empty())
10358+
if (inTemplateInstantiation())
1036010359
return;
1036110360

1036210361
// Scope isn't fine-grained enough to whitelist the specific cases, so
@@ -10951,7 +10950,7 @@ static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode(
1095110950
/// suppressed in the event of macro expansions.
1095210951
static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr,
1095310952
SourceLocation OpLoc) {
10954-
if (!S.ActiveTemplateInstantiations.empty())
10953+
if (S.inTemplateInstantiation())
1095510954
return;
1095610955
if (OpLoc.isInvalid() || OpLoc.isMacroID())
1095710956
return;

lib/Sema/SemaExprCXX.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType,
459459
if (E->getType()->isVariablyModifiedType())
460460
return ExprError(Diag(TypeidLoc, diag::err_variably_modified_typeid)
461461
<< E->getType());
462-
else if (ActiveTemplateInstantiations.empty() &&
462+
else if (!inTemplateInstantiation() &&
463463
E->HasSideEffects(Context, WasEvaluated)) {
464464
// The expression operand for typeid is in an unevaluated expression
465465
// context, so side effects could result in unintended consequences.
@@ -979,7 +979,7 @@ QualType Sema::getCurrentThisType() {
979979
}
980980

981981
if (ThisTy.isNull() && isLambdaCallOperator(CurContext) &&
982-
!ActiveTemplateInstantiations.empty()) {
982+
inTemplateInstantiation()) {
983983

984984
assert(isa<CXXRecordDecl>(DC) &&
985985
"Trying to get 'this' type from static method?");
@@ -6817,8 +6817,7 @@ ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand,
68176817
// The operand may have been modified when checking the placeholder type.
68186818
Operand = R.get();
68196819

6820-
if (ActiveTemplateInstantiations.empty() &&
6821-
Operand->HasSideEffects(Context, false)) {
6820+
if (!inTemplateInstantiation() && Operand->HasSideEffects(Context, false)) {
68226821
// The expression operand for noexcept is in an unevaluated expression
68236822
// context, so side effects could result in unintended consequences.
68246823
Diag(Operand->getExprLoc(), diag::warn_side_effects_unevaluated_context);

lib/Sema/SemaInit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity,
902902
// Don't warn during template instantiation. If the initialization was
903903
// non-dependent, we warned during the initial parse; otherwise, the
904904
// type might not be scalar in some uses of the template.
905-
if (!S.ActiveTemplateInstantiations.empty())
905+
if (S.inTemplateInstantiation())
906906
return;
907907

908908
unsigned DiagID = 0;
@@ -6249,7 +6249,7 @@ static void CheckMoveOnConstruction(Sema &S, const Expr *InitExpr,
62496249
if (!InitExpr)
62506250
return;
62516251

6252-
if (!S.ActiveTemplateInstantiations.empty())
6252+
if (S.inTemplateInstantiation())
62536253
return;
62546254

62556255
QualType DestType = InitExpr->getType();

lib/Sema/SemaLambda.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Sema::getCurrentMangleNumberContext(const DeclContext *DC,
312312
// In the following contexts [...] the one-definition rule requires closure
313313
// types in different translation units to "correspond":
314314
bool IsInNonspecializedTemplate =
315-
!ActiveTemplateInstantiations.empty() || CurContext->isDependentContext();
315+
inTemplateInstantiation() || CurContext->isDependentContext();
316316
switch (Kind) {
317317
case Normal: {
318318
// -- the bodies of non-exported nonspecialized template functions

lib/Sema/SemaOverload.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11491,7 +11491,7 @@ DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc,
1149111491
TemplateArgumentListInfo *ExplicitTemplateArgs,
1149211492
ArrayRef<Expr *> Args,
1149311493
bool *DoDiagnoseEmptyLookup = nullptr) {
11494-
if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty())
11494+
if (!SemaRef.inTemplateInstantiation() || !SS.isEmpty())
1149511495
return false;
1149611496

1149711497
for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) {

lib/Sema/SemaStmt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
18101810

18111811
D->setType(FirstType);
18121812

1813-
if (ActiveTemplateInstantiations.empty()) {
1813+
if (!inTemplateInstantiation()) {
18141814
SourceLocation Loc =
18151815
D->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
18161816
Diag(Loc, diag::warn_auto_var_is_id)

lib/Sema/SemaTemplate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6775,7 +6775,7 @@ static bool CheckTemplateSpecializationScope(Sema &S,
67756775
// Do not warn for class scope explicit specialization during
67766776
// instantiation, warning was already emitted during pattern
67776777
// semantic analysis.
6778-
if (!S.ActiveTemplateInstantiations.size())
6778+
if (!S.inTemplateInstantiation())
67796779
S.Diag(Loc, diag::ext_function_specialization_in_class)
67806780
<< Specialized;
67816781
} else {

lib/Sema/SemaType.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ static void diagnoseAndRemoveTypeQualifiers(Sema &S, const DeclSpec &DS,
742742
if (!(RemoveTQs & Qual.first))
743743
continue;
744744

745-
if (S.ActiveTemplateInstantiations.empty()) {
745+
if (!S.inTemplateInstantiation()) {
746746
if (TypeQuals & Qual.first)
747747
S.Diag(Qual.second, DiagID)
748748
<< DeclSpec::getSpecifierName(Qual.first) << TypeSoFar

0 commit comments

Comments
 (0)