clang 22.0.0git
State.cpp
Go to the documentation of this file.
1//===--- State.cpp - State chain for the VM and AST Walker ------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "State.h"
10#include "Frame.h"
11#include "Program.h"
15
16using namespace clang;
17using namespace clang::interp;
18
19State::~State() {}
20
22 unsigned ExtraNotes) {
23 return diag(Loc, DiagId, ExtraNotes, false);
24}
25
26OptionalDiagnostic State::FFDiag(const Expr *E, diag::kind DiagId,
27 unsigned ExtraNotes) {
28 if (getEvalStatus().Diag)
29 return diag(E->getExprLoc(), DiagId, ExtraNotes, false);
30 setActiveDiagnostic(false);
31 return OptionalDiagnostic();
32}
33
34OptionalDiagnostic State::FFDiag(const SourceInfo &SI, diag::kind DiagId,
35 unsigned ExtraNotes) {
36 if (getEvalStatus().Diag)
37 return diag(SI.getLoc(), DiagId, ExtraNotes, false);
38 setActiveDiagnostic(false);
39 return OptionalDiagnostic();
40}
41
43 unsigned ExtraNotes) {
44 // Don't override a previous diagnostic. Don't bother collecting
45 // diagnostics if we're evaluating for overflow.
46 if (!getEvalStatus().Diag || !getEvalStatus().Diag->empty()) {
47 setActiveDiagnostic(false);
48 return OptionalDiagnostic();
49 }
50 return diag(Loc, DiagId, ExtraNotes, true);
51}
52
53OptionalDiagnostic State::CCEDiag(const Expr *E, diag::kind DiagId,
54 unsigned ExtraNotes) {
55 return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes);
56}
57
58OptionalDiagnostic State::CCEDiag(const SourceInfo &SI, diag::kind DiagId,
59 unsigned ExtraNotes) {
60 return CCEDiag(SI.getLoc(), DiagId, ExtraNotes);
61}
62
64 if (!hasActiveDiagnostic())
65 return OptionalDiagnostic();
66 return OptionalDiagnostic(&addDiag(Loc, DiagId));
67}
68
69void State::addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
70 if (hasActiveDiagnostic())
71 llvm::append_range(*getEvalStatus().Diag, Diags);
72}
73
75 return getASTContext().getDiagnostics().Report(Loc, DiagId);
76}
77
78/// Add a diagnostic to the diagnostics list.
79PartialDiagnostic &State::addDiag(SourceLocation Loc, diag::kind DiagId) {
80 PartialDiagnostic PD(DiagId, getASTContext().getDiagAllocator());
81 getEvalStatus().Diag->push_back(std::make_pair(Loc, PD));
82 return getEvalStatus().Diag->back().second;
83}
84
86 unsigned ExtraNotes, bool IsCCEDiag) {
87 Expr::EvalStatus &EvalStatus = getEvalStatus();
88 if (EvalStatus.Diag) {
89 if (hasPriorDiagnostic()) {
90 return OptionalDiagnostic();
91 }
92
93 unsigned CallStackNotes = getCallStackDepth() - 1;
94 unsigned Limit =
95 getASTContext().getDiagnostics().getConstexprBacktraceLimit();
96 if (Limit)
97 CallStackNotes = std::min(CallStackNotes, Limit + 1);
98 if (checkingPotentialConstantExpression())
99 CallStackNotes = 0;
100
101 setActiveDiagnostic(true);
102 setFoldFailureDiagnostic(!IsCCEDiag);
103 EvalStatus.Diag->clear();
104 EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
105 addDiag(Loc, DiagId);
106 if (!checkingPotentialConstantExpression()) {
107 addCallStack(Limit);
108 }
109 return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
110 }
111 setActiveDiagnostic(false);
112 return OptionalDiagnostic();
113}
114
115const LangOptions &State::getLangOpts() const {
116 return getASTContext().getLangOpts();
117}
118
119void State::addCallStack(unsigned Limit) {
120 // Determine which calls to skip, if any.
121 unsigned ActiveCalls = getCallStackDepth() - 1;
122 unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
123 if (Limit && Limit < ActiveCalls) {
124 SkipStart = Limit / 2 + Limit % 2;
125 SkipEnd = ActiveCalls - Limit / 2;
126 }
127
128 // Walk the call stack and add the diagnostics.
129 unsigned CallIdx = 0;
130 const Frame *Top = getCurrentFrame();
131 const Frame *Bottom = getBottomFrame();
132 for (const Frame *F = Top; F != Bottom; F = F->getCaller(), ++CallIdx) {
133 SourceRange CallRange = F->getCallRange();
134 assert(CallRange.isValid());
135
136 // Skip this call?
137 if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
138 if (CallIdx == SkipStart) {
139 // Note that we're skipping calls.
140 addDiag(CallRange.getBegin(), diag::note_constexpr_calls_suppressed)
141 << unsigned(ActiveCalls - Limit);
142 }
143 continue;
144 }
145
146 // Use a different note for an inheriting constructor, because from the
147 // user's perspective it's not really a function at all.
148 if (const auto *CD =
149 dyn_cast_if_present<CXXConstructorDecl>(F->getCallee());
150 CD && CD->isInheritingConstructor()) {
151 addDiag(CallRange.getBegin(),
152 diag::note_constexpr_inherited_ctor_call_here)
153 << CD->getParent();
154 continue;
155 }
156
157 SmallString<128> Buffer;
158 llvm::raw_svector_ostream Out(Buffer);
159 F->describe(Out);
160 if (!Buffer.empty())
161 addDiag(CallRange.getBegin(), diag::note_constexpr_call_here)
162 << Out.str() << CallRange;
163 }
164}
Defines the clang::ASTContext interface.
Expr * E
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Implements a partial diagnostic which may not be emitted.
SourceLocation Loc
Definition: SemaObjC.cpp:754
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1233
This represents one expression.
Definition: Expr.h:112
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:273
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
A partial diagnostic which we might know in advance that we are not going to emit.
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
bool isValid() const
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
virtual Frame * getCaller() const =0
Returns a pointer to the caller frame.
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:73
SourceLocation getLoc() const
Definition: Source.cpp:15
The JSON file list parser is used to communicate input to InstallAPI.
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition: Expr.h:609
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition: Expr.h:633