1 //===--- State.cpp - State chain for the VM and AST Walker ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/CXXInheritance.h"
15 using namespace clang;
16 using namespace clang::interp;
20 OptionalDiagnostic State::FFDiag(SourceLocation Loc, diag::kind DiagId,
21 unsigned ExtraNotes) {
22 return diag(Loc, DiagId, ExtraNotes, false);
25 OptionalDiagnostic State::FFDiag(const Expr *E, diag::kind DiagId,
26 unsigned ExtraNotes) {
27 if (getEvalStatus().Diag)
28 return diag(E->getExprLoc(), DiagId, ExtraNotes, false);
29 setActiveDiagnostic(false);
30 return OptionalDiagnostic();
33 OptionalDiagnostic State::FFDiag(const SourceInfo &SI, diag::kind DiagId,
34 unsigned ExtraNotes) {
35 if (getEvalStatus().Diag)
36 return diag(SI.getLoc(), DiagId, ExtraNotes, false);
37 setActiveDiagnostic(false);
38 return OptionalDiagnostic();
41 OptionalDiagnostic State::CCEDiag(SourceLocation Loc, diag::kind DiagId,
42 unsigned ExtraNotes) {
43 // Don't override a previous diagnostic. Don't bother collecting
44 // diagnostics if we're evaluating for overflow.
45 if (!getEvalStatus().Diag || !getEvalStatus().Diag->empty()) {
46 setActiveDiagnostic(false);
47 return OptionalDiagnostic();
49 return diag(Loc, DiagId, ExtraNotes, true);
52 OptionalDiagnostic State::CCEDiag(const Expr *E, diag::kind DiagId,
53 unsigned ExtraNotes) {
54 return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes);
57 OptionalDiagnostic State::CCEDiag(const SourceInfo &SI, diag::kind DiagId,
58 unsigned ExtraNotes) {
59 return CCEDiag(SI.getLoc(), DiagId, ExtraNotes);
62 OptionalDiagnostic State::Note(SourceLocation Loc, diag::kind DiagId) {
63 if (!hasActiveDiagnostic())
64 return OptionalDiagnostic();
65 return OptionalDiagnostic(&addDiag(Loc, DiagId));
68 void State::addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
69 if (hasActiveDiagnostic()) {
70 getEvalStatus().Diag->insert(getEvalStatus().Diag->end(), Diags.begin(),
75 DiagnosticBuilder State::report(SourceLocation Loc, diag::kind DiagId) {
76 return getCtx().getDiagnostics().Report(Loc, DiagId);
79 /// Add a diagnostic to the diagnostics list.
80 PartialDiagnostic &State::addDiag(SourceLocation Loc, diag::kind DiagId) {
81 PartialDiagnostic PD(DiagId, getCtx().getDiagAllocator());
82 getEvalStatus().Diag->push_back(std::make_pair(Loc, PD));
83 return getEvalStatus().Diag->back().second;
86 OptionalDiagnostic State::diag(SourceLocation Loc, diag::kind DiagId,
87 unsigned ExtraNotes, bool IsCCEDiag) {
88 Expr::EvalStatus &EvalStatus = getEvalStatus();
89 if (EvalStatus.Diag) {
90 if (hasPriorDiagnostic()) {
91 return OptionalDiagnostic();
94 unsigned CallStackNotes = getCallStackDepth() - 1;
95 unsigned Limit = getCtx().getDiagnostics().getConstexprBacktraceLimit();
97 CallStackNotes = std::min(CallStackNotes, Limit + 1);
98 if (checkingPotentialConstantExpression())
101 setActiveDiagnostic(true);
102 setFoldFailureDiagnostic(!IsCCEDiag);
103 EvalStatus.Diag->clear();
104 EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
105 addDiag(Loc, DiagId);
106 if (!checkingPotentialConstantExpression()) {
109 return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
111 setActiveDiagnostic(false);
112 return OptionalDiagnostic();
115 const LangOptions &State::getLangOpts() const { return getCtx().getLangOpts(); }
117 void State::addCallStack(unsigned Limit) {
118 // Determine which calls to skip, if any.
119 unsigned ActiveCalls = getCallStackDepth() - 1;
120 unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
121 if (Limit && Limit < ActiveCalls) {
122 SkipStart = Limit / 2 + Limit % 2;
123 SkipEnd = ActiveCalls - Limit / 2;
126 // Walk the call stack and add the diagnostics.
127 unsigned CallIdx = 0;
128 Frame *Top = getCurrentFrame();
129 const Frame *Bottom = getBottomFrame();
130 for (Frame *F = Top; F != Bottom; F = F->getCaller(), ++CallIdx) {
131 SourceLocation CallLocation = F->getCallLocation();
134 if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
135 if (CallIdx == SkipStart) {
136 // Note that we're skipping calls.
137 addDiag(CallLocation, diag::note_constexpr_calls_suppressed)
138 << unsigned(ActiveCalls - Limit);
143 // Use a different note for an inheriting constructor, because from the
144 // user's perspective it's not really a function at all.
145 if (auto *CD = dyn_cast_or_null<CXXConstructorDecl>(F->getCallee())) {
146 if (CD->isInheritingConstructor()) {
147 addDiag(CallLocation, diag::note_constexpr_inherited_ctor_call_here)
153 SmallString<128> Buffer;
154 llvm::raw_svector_ostream Out(Buffer);
156 addDiag(CallLocation, diag::note_constexpr_call_here) << Out.str();