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
20
22 unsigned ExtraNotes) {
23 return diag(Loc, DiagId, ExtraNotes, false);
24}
25
27 unsigned ExtraNotes) {
28 if (getEvalStatus().Diag)
29 return diag(E->getExprLoc(), DiagId, ExtraNotes, false);
31 return OptionalDiagnostic();
32}
33
35 unsigned ExtraNotes) {
36 if (getEvalStatus().Diag)
37 return diag(SI.getLoc(), DiagId, ExtraNotes, 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()) {
48 return OptionalDiagnostic();
49 }
50 return diag(Loc, DiagId, ExtraNotes, true);
51}
52
54 unsigned ExtraNotes) {
55 return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes);
56}
57
59 unsigned ExtraNotes) {
60 return CCEDiag(SI.getLoc(), DiagId, ExtraNotes);
61}
62
65 return OptionalDiagnostic();
66 return OptionalDiagnostic(&addDiag(Loc, DiagId));
67}
68
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
85OptionalDiagnostic State::diag(SourceLocation Loc, diag::kind DiagId,
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
115void State::addCallStack(unsigned Limit) {
116 // Determine which calls to skip, if any.
117 unsigned ActiveCalls = getCallStackDepth() - 1;
118 unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
119 if (Limit && Limit < ActiveCalls) {
120 SkipStart = Limit / 2 + Limit % 2;
121 SkipEnd = ActiveCalls - Limit / 2;
122 }
123
124 // Walk the call stack and add the diagnostics.
125 unsigned CallIdx = 0;
126 const Frame *Top = getCurrentFrame();
127 const Frame *Bottom = getBottomFrame();
128 for (const Frame *F = Top; F != Bottom; F = F->getCaller(), ++CallIdx) {
129 SourceRange CallRange = F->getCallRange();
130 assert(CallRange.isValid());
131
132 // Skip this call?
133 if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
134 if (CallIdx == SkipStart) {
135 // Note that we're skipping calls.
136 addDiag(CallRange.getBegin(), diag::note_constexpr_calls_suppressed)
137 << unsigned(ActiveCalls - Limit);
138 }
139 continue;
140 }
141
142 // Use a different note for an inheriting constructor, because from the
143 // user's perspective it's not really a function at all.
144 if (const auto *CD =
145 dyn_cast_if_present<CXXConstructorDecl>(F->getCallee());
146 CD && CD->isInheritingConstructor()) {
147 addDiag(CallRange.getBegin(),
148 diag::note_constexpr_inherited_ctor_call_here)
149 << CD->getParent();
150 continue;
151 }
152
153 SmallString<128> Buffer;
154 llvm::raw_svector_ostream Out(Buffer);
155 F->describe(Out);
156 if (!Buffer.empty())
157 addDiag(CallRange.getBegin(), diag::note_constexpr_call_here)
158 << Out.str() << CallRange;
159 }
160}
Defines the clang::ASTContext interface.
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.
A little helper class used to produce diagnostics.
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
A partial diagnostic which we might know in advance that we are not going to emit.
Encodes a location in the source.
SourceLocation getBegin() const
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
virtual bool hasActiveDiagnostic()=0
virtual void setActiveDiagnostic(bool Flag)=0
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:63
DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId)
Directly reports a diagnostic message.
Definition State.cpp:74
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
virtual Expr::EvalStatus & getEvalStatus() const =0
void addNotes(ArrayRef< PartialDiagnosticAt > Diags)
Add a stack of notes to a prior diagnostic.
Definition State.cpp:69
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:42
virtual ASTContext & getASTContext() const =0
virtual ~State()
Definition State.cpp:19
unsigned kind
All of the diagnostics that can be emitted by the frontend.
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