1#include "clang/Sema/SemaBase.h"
2#include "clang/Sema/Sema.h"
3#include "clang/Sema/SemaCUDA.h"
4
5namespace clang {
6
7SemaBase::SemaBase(Sema &S) : SemaRef(S) {}
8
9ASTContext &SemaBase::getASTContext() const { return SemaRef.Context; }
10DiagnosticsEngine &SemaBase::getDiagnostics() const { return SemaRef.Diags; }
11const LangOptions &SemaBase::getLangOpts() const { return SemaRef.LangOpts; }
12DeclContext *SemaBase::getCurContext() const { return SemaRef.CurContext; }
13
14SemaBase::ImmediateDiagBuilder::~ImmediateDiagBuilder() {
15 // If we aren't active, there is nothing to do.
16 if (!isActive())
17 return;
18
19 // Otherwise, we need to emit the diagnostic. First clear the diagnostic
20 // builder itself so it won't emit the diagnostic in its own destructor.
21 //
22 // This seems wasteful, in that as written the DiagnosticBuilder dtor will
23 // do its own needless checks to see if the diagnostic needs to be
24 // emitted. However, because we take care to ensure that the builder
25 // objects never escape, a sufficiently smart compiler will be able to
26 // eliminate that code.
27 Clear();
28
29 // Dispatch to Sema to emit the diagnostic.
30 SemaRef.EmitDiagnostic(DiagID, DB: *this);
31}
32
33PartialDiagnostic SemaBase::PDiag(unsigned DiagID) {
34 return PartialDiagnostic(DiagID, SemaRef.Context.getDiagAllocator());
35}
36
37const SemaBase::SemaDiagnosticBuilder &
38operator<<(const SemaBase::SemaDiagnosticBuilder &Diag,
39 const PartialDiagnostic &PD) {
40 if (Diag.ImmediateDiag)
41 PD.Emit(DB: *Diag.ImmediateDiag);
42 else if (Diag.PartialDiagId)
43 Diag.S.DeviceDeferredDiags[Diag.Fn][*Diag.PartialDiagId].second = PD;
44 return Diag;
45}
46
47void SemaBase::SemaDiagnosticBuilder::AddFixItHint(
48 const FixItHint &Hint) const {
49 if (ImmediateDiag)
50 ImmediateDiag->AddFixItHint(Hint);
51 else if (PartialDiagId)
52 S.DeviceDeferredDiags[Fn][*PartialDiagId].second.AddFixItHint(Hint);
53}
54
55llvm::DenseMap<CanonicalDeclPtr<const FunctionDecl>,
56 std::vector<PartialDiagnosticAt>> &
57SemaBase::SemaDiagnosticBuilder::getDeviceDeferredDiags() const {
58 return S.DeviceDeferredDiags;
59}
60
61Sema::SemaDiagnosticBuilder SemaBase::Diag(SourceLocation Loc,
62 unsigned DiagID) {
63 bool IsError =
64 getDiagnostics().getDiagnosticIDs()->isDefaultMappingAsError(DiagID);
65 bool ShouldDefer = getLangOpts().CUDA && getLangOpts().GPUDeferDiag &&
66 DiagnosticIDs::isDeferrable(DiagID) &&
67 (SemaRef.DeferDiags || !IsError);
68 // Even without -fgpu-defer-diag, defer device-side errors inside an
69 // implicit-H+D explicit instantiation so end-of-TU classification can
70 // choose between surfacing them or emitting a trap body.
71 if (!ShouldDefer && getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
72 DiagnosticIDs::isDeferrable(DiagID) &&
73 SemaCUDA::isImplicitHDExplicitInstantiation(
74 FD: SemaRef.getCurFunctionDecl(/*AllowLambda=*/true)))
75 ShouldDefer = true;
76 auto SetIsLastErrorImmediate = [&](bool Flag) {
77 if (IsError)
78 SemaRef.IsLastErrorImmediate = Flag;
79 };
80 if (!ShouldDefer) {
81 SetIsLastErrorImmediate(true);
82 return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc,
83 DiagID, SemaRef.getCurFunctionDecl(), SemaRef);
84 }
85
86 SemaDiagnosticBuilder DB = getLangOpts().CUDAIsDevice
87 ? SemaRef.CUDA().DiagIfDeviceCode(Loc, DiagID)
88 : SemaRef.CUDA().DiagIfHostCode(Loc, DiagID);
89 SetIsLastErrorImmediate(DB.isImmediate());
90 return DB;
91}
92
93Sema::SemaDiagnosticBuilder SemaBase::Diag(SourceLocation Loc,
94 const PartialDiagnostic &PD) {
95 return Diag(Loc, DiagID: PD.getDiagID()) << PD;
96}
97
98SemaBase::SemaDiagnosticBuilder SemaBase::DiagCompat(SourceLocation Loc,
99 unsigned CompatDiagId) {
100 return Diag(Loc,
101 DiagID: DiagnosticIDs::getCXXCompatDiagId(LangOpts: getLangOpts(), CompatDiagId));
102}
103} // namespace clang
104