clang 24.0.0git
SemaBase.h
Go to the documentation of this file.
1//===--- SemaBase.h - Common utilities for semantic analysis-----*- 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// This file defines the SemaBase class, which provides utilities for Sema
10// and its parts like SemaOpenACC.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SEMA_SEMABASE_H
15#define LLVM_CLANG_SEMA_SEMABASE_H
16
17#include "clang/AST/Decl.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/DenseMapInfo.h"
25#include "llvm/ADT/Hashing.h"
26#include <optional>
27#include <type_traits>
28#include <utility>
29#include <vector>
30
31namespace clang {
32
33class ASTContext;
35class LangOptions;
36class Sema;
37
38class SemaBase {
39public:
40 SemaBase(Sema &S);
41
42 /// A pair of a canonical FunctionDecl and a SourceLocation. When used as the
43 /// key in a hashtable, both the FD and location are hashed.
48
50
53 const LangOptions &getLangOpts() const;
55
56 /// Helper class that creates diagnostics with optional
57 /// template instantiation stacks.
58 ///
59 /// This class provides a wrapper around the basic DiagnosticBuilder
60 /// class that emits diagnostics. ImmediateDiagBuilder is
61 /// responsible for emitting the diagnostic (as DiagnosticBuilder
62 /// does) and, if the diagnostic comes from inside a template
63 /// instantiation, printing the template instantiation stack as
64 /// well.
65 class ImmediateDiagBuilder : public DiagnosticBuilder {
66 Sema &SemaRef;
67 unsigned DiagID;
68
69 public:
70 ImmediateDiagBuilder(DiagnosticBuilder &DB, Sema &SemaRef, unsigned DiagID)
71 : DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {}
72 ImmediateDiagBuilder(DiagnosticBuilder &&DB, Sema &SemaRef, unsigned DiagID)
73 : DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {}
74
75 // This is a cunning lie. DiagnosticBuilder actually performs move
76 // construction in its copy constructor (but due to varied uses, it's not
77 // possible to conveniently express this as actual move construction). So
78 // the default copy ctor here is fine, because the base class disables the
79 // source anyway, so the user-defined ~ImmediateDiagBuilder is a safe no-op
80 // in that case anwyay.
82
84
85 /// Teach operator<< to produce an object of the correct type.
86 template <typename T>
87 friend const ImmediateDiagBuilder &
89 const DiagnosticBuilder &BaseDiag = Diag;
90 BaseDiag << Value;
91 return Diag;
92 }
93
94 // It is necessary to limit this to rvalue reference to avoid calling this
95 // function with a bitfield lvalue argument since non-const reference to
96 // bitfield is not allowed.
97 template <typename T,
98 typename = std::enable_if_t<!std::is_lvalue_reference<T>::value>>
100 const DiagnosticBuilder &BaseDiag = *this;
101 BaseDiag << std::move(V);
102 return *this;
103 }
104 };
105
106 /// A generic diagnostic builder for errors which may or may not be deferred.
107 ///
108 /// In CUDA, there exist constructs (e.g. variable-length arrays, try/catch)
109 /// which are not allowed to appear inside __device__ functions and are
110 /// allowed to appear in __host__ __device__ functions only if the host+device
111 /// function is never codegen'ed.
112 ///
113 /// To handle this, we use the notion of "deferred diagnostics", where we
114 /// attach a diagnostic to a FunctionDecl that's emitted iff it's codegen'ed.
115 ///
116 /// This class lets you emit either a regular diagnostic, a deferred
117 /// diagnostic, or no diagnostic at all, according to an argument you pass to
118 /// its constructor, thus simplifying the process of creating these "maybe
119 /// deferred" diagnostics.
121 public:
122 enum Kind {
123 /// Emit no diagnostics.
125 /// Emit the diagnostic immediately (i.e., behave like Sema::Diag()).
127 /// Emit the diagnostic immediately, and, if it's a warning or error, also
128 /// emit a call stack showing how this function can be reached by an a
129 /// priori known-emitted function.
131 /// Create a deferred diagnostic, which is emitted only if the function
132 /// it's attached to is codegen'ed. Also emit a call stack as with
133 /// K_ImmediateWithCallStack.
135 };
136
138 const FunctionDecl *Fn, Sema &S);
141
142 // The copy and move assignment operator is defined as deleted pending
143 // further motivation.
146
148
149 bool isImmediate() const { return ImmediateDiag.has_value(); }
150
151 /// Convertible to bool: True if we immediately emitted an error, false if
152 /// we didn't emit an error or we created a deferred error.
153 ///
154 /// Example usage:
155 ///
156 /// if (SemaDiagnosticBuilder(...) << foo << bar)
157 /// return ExprError();
158 ///
159 /// But see DiagIfDeviceCode() and DiagIfHostCode() -- you probably
160 /// want to use these instead of creating a SemaDiagnosticBuilder yourself.
161 operator bool() const { return isImmediate(); }
162
163 template <typename T>
166 if (Diag.ImmediateDiag)
167 *Diag.ImmediateDiag << Value;
168 else if (Diag.PartialDiagId)
169 Diag.getDeviceDeferredDiags()[Diag.Fn][*Diag.PartialDiagId].second
170 << Value;
171 return Diag;
172 }
173
174 // It is necessary to limit this to rvalue reference to avoid calling this
175 // function with a bitfield lvalue argument since non-const reference to
176 // bitfield is not allowed.
177 template <typename T,
178 typename = std::enable_if_t<!std::is_lvalue_reference<T>::value>>
180 if (ImmediateDiag)
181 *ImmediateDiag << std::move(V);
182 else if (PartialDiagId)
183 getDeviceDeferredDiags()[Fn][*PartialDiagId].second << std::move(V);
184 return *this;
185 }
186
187 friend const SemaDiagnosticBuilder &
189
190 void AddFixItHint(const FixItHint &Hint) const;
191
193 return ExprError();
194 }
196 return StmtError();
197 }
198 operator ExprResult() const { return ExprError(); }
199 operator StmtResult() const { return StmtError(); }
200 operator TypeResult() const { return TypeError(); }
201 operator DeclResult() const { return DeclResult(true); }
202 operator MemInitResult() const { return MemInitResult(true); }
203
205 llvm::DenseMap<CanonicalDeclPtr<const FunctionDecl>,
206 std::vector<PartialDiagnosticAt>>;
207
208 private:
209 Sema &S;
210 SourceLocation Loc;
211 unsigned DiagID;
212 const FunctionDecl *Fn;
213 bool ShowCallStack;
214
215 // Invariant: At most one of these Optionals has a value.
216 // FIXME: Switch these to a Variant once that exists.
217 std::optional<ImmediateDiagBuilder> ImmediateDiag;
218 std::optional<unsigned> PartialDiagId;
219
220 DeferredDiagnosticsType &getDeviceDeferredDiags() const;
221 };
222
223 /// Emit a diagnostic.
224 SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
225
226 /// Emit a partial diagnostic.
228
229 /// Emit a compatibility diagnostic.
230 SemaDiagnosticBuilder DiagCompat(SourceLocation Loc, unsigned CompatDiagId);
231
232 /// Build a partial diagnostic.
233 PartialDiagnostic PDiag(unsigned DiagID = 0);
234};
235
236} // namespace clang
237
238namespace llvm {
239// Hash a FunctionDeclAndLoc by looking at both its FunctionDecl and its
240// SourceLocation.
241template <> struct DenseMapInfo<clang::SemaBase::FunctionDeclAndLoc> {
244 DenseMapInfo<clang::CanonicalDeclPtr<const clang::FunctionDecl>>;
245
246 static unsigned getHashValue(const FunctionDeclAndLoc &FDL) {
247 return hash_combine(FDBaseInfo::getHashValue(FDL.FD),
248 FDL.Loc.getHashValue());
249 }
250
251 static bool isEqual(const FunctionDeclAndLoc &LHS,
252 const FunctionDeclAndLoc &RHS) {
253 return LHS.FD == RHS.FD && LHS.Loc == RHS.Loc;
254 }
255};
256} // namespace llvm
257
258#endif
#define V(N, I)
Defines the Diagnostic-related interfaces.
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream.
Defines the clang::SourceLocation class and associated facilities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
A wrapper class around a pointer that always points to its canonical declaration.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1467
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:240
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition Diagnostic.h:78
Represents a function declaration or definition.
Definition Decl.h:2082
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
ImmediateDiagBuilder(DiagnosticBuilder &&DB, Sema &SemaRef, unsigned DiagID)
Definition SemaBase.h:72
ImmediateDiagBuilder(const ImmediateDiagBuilder &)=default
const ImmediateDiagBuilder & operator<<(T &&V) const
Definition SemaBase.h:99
friend const ImmediateDiagBuilder & operator<<(const ImmediateDiagBuilder &Diag, const T &Value)
Teach operator<< to produce an object of the correct type.
Definition SemaBase.h:88
ImmediateDiagBuilder(DiagnosticBuilder &DB, Sema &SemaRef, unsigned DiagID)
Definition SemaBase.h:70
A generic diagnostic builder for errors which may or may not be deferred.
Definition SemaBase.h:120
SemaDiagnosticBuilder(const SemaDiagnosticBuilder &)=default
llvm::DenseMap< CanonicalDeclPtr< const FunctionDecl >, std::vector< PartialDiagnosticAt > > DeferredDiagnosticsType
Definition SemaBase.h:204
@ K_Deferred
Create a deferred diagnostic, which is emitted only if the function it's attached to is codegen'ed.
Definition SemaBase.h:134
@ K_ImmediateWithCallStack
Emit the diagnostic immediately, and, if it's a warning or error, also emit a call stack showing how ...
Definition SemaBase.h:130
@ K_Immediate
Emit the diagnostic immediately (i.e., behave like Sema::Diag()).
Definition SemaBase.h:126
void AddFixItHint(const FixItHint &Hint) const
Definition SemaBase.cpp:47
friend const SemaDiagnosticBuilder & operator<<(const SemaDiagnosticBuilder &Diag, const T &Value)
Definition SemaBase.h:165
SemaDiagnosticBuilder(Kind K, SourceLocation Loc, unsigned DiagID, const FunctionDecl *Fn, Sema &S)
friend StmtResult StmtError(const SemaDiagnosticBuilder &)
Definition SemaBase.h:195
const SemaDiagnosticBuilder & operator<<(T &&V) const
Definition SemaBase.h:179
SemaDiagnosticBuilder & operator=(const SemaDiagnosticBuilder &)=delete
friend ExprResult ExprError(const SemaDiagnosticBuilder &)
Definition SemaBase.h:192
SemaDiagnosticBuilder & operator=(SemaDiagnosticBuilder &&)=delete
SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D)
PartialDiagnostic PDiag(unsigned DiagID=0)
Build a partial diagnostic.
Definition SemaBase.cpp:33
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
Sema & SemaRef
Definition SemaBase.h:49
const LangOptions & getLangOpts() const
Definition SemaBase.cpp:11
DiagnosticsEngine & getDiagnostics() const
Definition SemaBase.cpp:10
SemaDiagnosticBuilder DiagCompat(SourceLocation Loc, unsigned CompatDiagId)
Emit a compatibility diagnostic.
Definition SemaBase.cpp:98
DeclContext * getCurContext() const
Definition SemaBase.cpp:12
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
Encodes a location in the source.
unsigned getHashValue() const
Top level wrappers for InstallAPI frontend operations.
ActionResult< Decl * > DeclResult
Definition Ownership.h:255
TypeResult TypeError()
Definition Ownership.h:267
StmtResult StmtError()
Definition Ownership.h:266
ActionResult< ParsedType > TypeResult
Definition Ownership.h:251
ActionResult< CXXCtorInitializer * > MemInitResult
Definition Ownership.h:253
const FunctionProtoType * T
ExprResult ExprError()
Definition Ownership.h:265
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
ActionResult< Stmt * > StmtResult
Definition Ownership.h:250
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
A pair of a canonical FunctionDecl and a SourceLocation.
Definition SemaBase.h:44
CanonicalDeclPtr< const FunctionDecl > FD
Definition SemaBase.h:45
static unsigned getHashValue(const FunctionDeclAndLoc &FDL)
Definition SemaBase.h:246
static bool isEqual(const FunctionDeclAndLoc &LHS, const FunctionDeclAndLoc &RHS)
Definition SemaBase.h:251
clang::SemaBase::FunctionDeclAndLoc FunctionDeclAndLoc
Definition SemaBase.h:242
DenseMapInfo< clang::CanonicalDeclPtr< const clang::FunctionDecl > > FDBaseInfo
Definition SemaBase.h:243