clang  19.0.0git
SemaBase.cpp
Go to the documentation of this file.
1 #include "clang/Sema/SemaBase.h"
2 #include "clang/Sema/Sema.h"
3 #include "clang/Sema/SemaCUDA.h"
4 
5 namespace clang {
6 
7 SemaBase::SemaBase(Sema &S) : SemaRef(S) {}
8 
12 
14  // If we aren't active, there is nothing to do.
15  if (!isActive())
16  return;
17 
18  // Otherwise, we need to emit the diagnostic. First clear the diagnostic
19  // builder itself so it won't emit the diagnostic in its own destructor.
20  //
21  // This seems wasteful, in that as written the DiagnosticBuilder dtor will
22  // do its own needless checks to see if the diagnostic needs to be
23  // emitted. However, because we take care to ensure that the builder
24  // objects never escape, a sufficiently smart compiler will be able to
25  // eliminate that code.
26  Clear();
27 
28  // Dispatch to Sema to emit the diagnostic.
29  SemaRef.EmitCurrentDiagnostic(DiagID);
30 }
31 
34  const PartialDiagnostic &PD) {
35  if (Diag.ImmediateDiag)
36  PD.Emit(*Diag.ImmediateDiag);
37  else if (Diag.PartialDiagId)
38  Diag.S.DeviceDeferredDiags[Diag.Fn][*Diag.PartialDiagId].getDiag().second =
39  PD;
40  return Diag;
41 }
42 
44  const FixItHint &Hint) const {
45  if (ImmediateDiag)
46  ImmediateDiag->AddFixItHint(Hint);
47  else if (PartialDiagId)
48  S.DeviceDeferredDiags[Fn][*PartialDiagId].getDiag().second.AddFixItHint(
49  Hint);
50 }
51 
53 SemaBase::SemaDiagnosticBuilder::getDeviceDeferredDiags() const {
54  return S.DeviceDeferredDiags;
55 }
56 
58  bool DeferHint) {
59  bool IsError =
60  getDiagnostics().getDiagnosticIDs()->isDefaultMappingAsError(DiagID);
61  bool ShouldDefer = getLangOpts().CUDA && getLangOpts().GPUDeferDiag &&
63  (DeferHint || SemaRef.DeferDiags || !IsError);
64  auto SetIsLastErrorImmediate = [&](bool Flag) {
65  if (IsError)
67  };
68  if (!ShouldDefer) {
69  SetIsLastErrorImmediate(true);
73  }
74 
75  SemaDiagnosticBuilder DB = getLangOpts().CUDAIsDevice
76  ? SemaRef.CUDA().DiagIfDeviceCode(Loc, DiagID)
77  : SemaRef.CUDA().DiagIfHostCode(Loc, DiagID);
78  SetIsLastErrorImmediate(DB.isImmediate());
79  return DB;
80 }
81 
83  const PartialDiagnostic &PD,
84  bool DeferHint) {
85  return Diag(Loc, PD.getDiagID(), DeferHint) << PD;
86 }
87 
88 } // namespace clang
This file declares semantic analysis for CUDA constructs.
SourceLocation Loc
Definition: SemaObjC.cpp:755
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
void Clear() const
Clear out the current diagnostic.
Definition: Diagnostic.h:1309
bool isActive() const
Determine whether this diagnostic is still active.
Definition: Diagnostic.h:1316
static bool isDeferrable(unsigned DiagID)
Whether the diagnostic message can be deferred.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:193
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:563
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:72
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
void Emit(const DiagnosticBuilder &DB) const
A generic diagnostic builder for errors which may or may not be deferred.
Definition: SemaBase.h:175
@ K_Immediate
Emit the diagnostic immediately (i.e., behave like Sema::Diag()).
Definition: SemaBase.h:181
void AddFixItHint(const FixItHint &Hint) const
Definition: SemaBase.cpp:43
llvm::DenseMap< CanonicalDeclPtr< const FunctionDecl >, std::vector< DeviceDeferredDiagnostic > > DeferredDiagnosticsType
Definition: SemaBase.h:264
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:57
@ All
A flag representing 'all'.
SemaBase(Sema &S)
Definition: SemaBase.cpp:7
ASTContext & getASTContext() const
Definition: SemaBase.cpp:9
Sema & SemaRef
Definition: SemaBase.h:40
const LangOptions & getLangOpts() const
Definition: SemaBase.cpp:11
DiagnosticsEngine & getDiagnostics() const
Definition: SemaBase.cpp:10
SemaDiagnosticBuilder DiagIfHostCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as host cod...
Definition: SemaCUDA.cpp:927
SemaDiagnosticBuilder DiagIfDeviceCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as device c...
Definition: SemaCUDA.cpp:894
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:462
bool IsLastErrorImmediate
Is the last error level diagnostic immediate.
Definition: Sema.h:923
FunctionDecl * getCurFunctionDecl(bool AllowLambda=false) const
Returns a pointer to the innermost enclosing function, or nullptr if the current context is not insid...
Definition: Sema.cpp:1547
ASTContext & Context
Definition: Sema.h:857
SemaDiagnosticBuilder::DeferredDiagnosticsType DeviceDeferredDiags
Diagnostics that are emitted only if we discover that the given function must be codegen'ed.
Definition: Sema.h:992
void EmitCurrentDiagnostic(unsigned DiagID)
Cause the active diagnostic on the DiagosticsEngine to be emitted.
Definition: Sema.cpp:1572
const LangOptions & LangOpts
Definition: Sema.h:855
bool DeferDiags
Whether deferrable diagnostics should be deferred.
Definition: Sema.h:8024
DiagnosticsEngine & Diags
Definition: Sema.h:859
SemaCUDA & CUDA()
Definition: Sema.h:1002
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.