Bug Summary

File:tools/clang/include/clang/Basic/PartialDiagnostic.h
Warning:line 212, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DeclBase.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn362543/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn362543=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-06-05-060531-1271-1 -x c++ /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp -faddrsig

/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp

1//===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
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 implements the Decl and DeclContext classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/DeclBase.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTMutationListener.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/AttrIterator.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclContextInternals.h"
21#include "clang/AST/DeclFriend.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclOpenMP.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/DependentDiagnostic.h"
26#include "clang/AST/ExternalASTSource.h"
27#include "clang/AST/Stmt.h"
28#include "clang/AST/Type.h"
29#include "clang/Basic/IdentifierTable.h"
30#include "clang/Basic/LLVM.h"
31#include "clang/Basic/LangOptions.h"
32#include "clang/Basic/ObjCRuntime.h"
33#include "clang/Basic/PartialDiagnostic.h"
34#include "clang/Basic/SourceLocation.h"
35#include "clang/Basic/TargetInfo.h"
36#include "llvm/ADT/ArrayRef.h"
37#include "llvm/ADT/PointerIntPair.h"
38#include "llvm/ADT/SmallVector.h"
39#include "llvm/ADT/StringRef.h"
40#include "llvm/Support/Casting.h"
41#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/MathExtras.h"
43#include "llvm/Support/VersionTuple.h"
44#include "llvm/Support/raw_ostream.h"
45#include <algorithm>
46#include <cassert>
47#include <cstddef>
48#include <string>
49#include <tuple>
50#include <utility>
51
52using namespace clang;
53
54//===----------------------------------------------------------------------===//
55// Statistics
56//===----------------------------------------------------------------------===//
57
58#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
59#define ABSTRACT_DECL(DECL)
60#include "clang/AST/DeclNodes.inc"
61
62void Decl::updateOutOfDate(IdentifierInfo &II) const {
63 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
64}
65
66#define DECL(DERIVED, BASE) \
67 static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \
68 "Alignment sufficient after objects prepended to " #DERIVED);
69#define ABSTRACT_DECL(DECL)
70#include "clang/AST/DeclNodes.inc"
71
72void *Decl::operator new(std::size_t Size, const ASTContext &Context,
73 unsigned ID, std::size_t Extra) {
74 // Allocate an extra 8 bytes worth of storage, which ensures that the
75 // resulting pointer will still be 8-byte aligned.
76 static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
77 "Decl won't be misaligned");
78 void *Start = Context.Allocate(Size + Extra + 8);
79 void *Result = (char*)Start + 8;
80
81 unsigned *PrefixPtr = (unsigned *)Result - 2;
82
83 // Zero out the first 4 bytes; this is used to store the owning module ID.
84 PrefixPtr[0] = 0;
85
86 // Store the global declaration ID in the second 4 bytes.
87 PrefixPtr[1] = ID;
88
89 return Result;
90}
91
92void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
93 DeclContext *Parent, std::size_t Extra) {
94 assert(!Parent || &Parent->getParentASTContext() == &Ctx)((!Parent || &Parent->getParentASTContext() == &Ctx
) ? static_cast<void> (0) : __assert_fail ("!Parent || &Parent->getParentASTContext() == &Ctx"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 94, __PRETTY_FUNCTION__))
;
95 // With local visibility enabled, we track the owning module even for local
96 // declarations. We create the TU decl early and may not yet know what the
97 // LangOpts are, so conservatively allocate the storage.
98 if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) {
99 // Ensure required alignment of the resulting object by adding extra
100 // padding at the start if required.
101 size_t ExtraAlign =
102 llvm::OffsetToAlignment(sizeof(Module *), alignof(Decl));
103 auto *Buffer = reinterpret_cast<char *>(
104 ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
105 Buffer += ExtraAlign;
106 auto *ParentModule =
107 Parent ? cast<Decl>(Parent)->getOwningModule() : nullptr;
108 return new (Buffer) Module*(ParentModule) + 1;
109 }
110 return ::operator new(Size + Extra, Ctx);
111}
112
113Module *Decl::getOwningModuleSlow() const {
114 assert(isFromASTFile() && "Not from AST file?")((isFromASTFile() && "Not from AST file?") ? static_cast
<void> (0) : __assert_fail ("isFromASTFile() && \"Not from AST file?\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 114, __PRETTY_FUNCTION__))
;
115 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
116}
117
118bool Decl::hasLocalOwningModuleStorage() const {
119 return getASTContext().getLangOpts().trackLocalOwningModule();
120}
121
122const char *Decl::getDeclKindName() const {
123 switch (DeclKind) {
124 default: llvm_unreachable("Declaration not in DeclNodes.inc!")::llvm::llvm_unreachable_internal("Declaration not in DeclNodes.inc!"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 124)
;
125#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
126#define ABSTRACT_DECL(DECL)
127#include "clang/AST/DeclNodes.inc"
128 }
129}
130
131void Decl::setInvalidDecl(bool Invalid) {
132 InvalidDecl = Invalid;
133 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition())((!isa<TagDecl>(this) || !cast<TagDecl>(this)->
isCompleteDefinition()) ? static_cast<void> (0) : __assert_fail
("!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition()"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 133, __PRETTY_FUNCTION__))
;
134 if (!Invalid) {
135 return;
136 }
137
138 if (!isa<ParmVarDecl>(this)) {
139 // Defensive maneuver for ill-formed code: we're likely not to make it to
140 // a point where we set the access specifier, so default it to "public"
141 // to avoid triggering asserts elsewhere in the front end.
142 setAccess(AS_public);
143 }
144
145 // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
146 // are invalid too.
147 if (auto *DD = dyn_cast<DecompositionDecl>(this)) {
148 for (auto *Binding : DD->bindings()) {
149 Binding->setInvalidDecl();
150 }
151 }
152}
153
154const char *DeclContext::getDeclKindName() const {
155 switch (getDeclKind()) {
156#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
157#define ABSTRACT_DECL(DECL)
158#include "clang/AST/DeclNodes.inc"
159 }
160 llvm_unreachable("Declaration context not in DeclNodes.inc!")::llvm::llvm_unreachable_internal("Declaration context not in DeclNodes.inc!"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 160)
;
161}
162
163bool Decl::StatisticsEnabled = false;
164void Decl::EnableStatistics() {
165 StatisticsEnabled = true;
166}
167
168void Decl::PrintStats() {
169 llvm::errs() << "\n*** Decl Stats:\n";
170
171 int totalDecls = 0;
172#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
173#define ABSTRACT_DECL(DECL)
174#include "clang/AST/DeclNodes.inc"
175 llvm::errs() << " " << totalDecls << " decls total.\n";
176
177 int totalBytes = 0;
178#define DECL(DERIVED, BASE) \
179 if (n##DERIVED##s > 0) { \
180 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
181 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
182 << sizeof(DERIVED##Decl) << " each (" \
183 << n##DERIVED##s * sizeof(DERIVED##Decl) \
184 << " bytes)\n"; \
185 }
186#define ABSTRACT_DECL(DECL)
187#include "clang/AST/DeclNodes.inc"
188
189 llvm::errs() << "Total bytes = " << totalBytes << "\n";
190}
191
192void Decl::add(Kind k) {
193 switch (k) {
194#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
195#define ABSTRACT_DECL(DECL)
196#include "clang/AST/DeclNodes.inc"
197 }
198}
199
200bool Decl::isTemplateParameterPack() const {
201 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this))
202 return TTP->isParameterPack();
203 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this))
204 return NTTP->isParameterPack();
205 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this))
206 return TTP->isParameterPack();
207 return false;
208}
209
210bool Decl::isParameterPack() const {
211 if (const auto *Var = dyn_cast<VarDecl>(this))
212 return Var->isParameterPack();
213
214 return isTemplateParameterPack();
215}
216
217FunctionDecl *Decl::getAsFunction() {
218 if (auto *FD = dyn_cast<FunctionDecl>(this))
219 return FD;
220 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
221 return FTD->getTemplatedDecl();
222 return nullptr;
223}
224
225bool Decl::isTemplateDecl() const {
226 return isa<TemplateDecl>(this);
227}
228
229TemplateDecl *Decl::getDescribedTemplate() const {
230 if (auto *FD = dyn_cast<FunctionDecl>(this))
231 return FD->getDescribedFunctionTemplate();
232 else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
233 return RD->getDescribedClassTemplate();
234 else if (auto *VD = dyn_cast<VarDecl>(this))
235 return VD->getDescribedVarTemplate();
236 else if (auto *AD = dyn_cast<TypeAliasDecl>(this))
237 return AD->getDescribedAliasTemplate();
238
239 return nullptr;
240}
241
242bool Decl::isTemplated() const {
243 // A declaration is dependent if it is a template or a template pattern, or
244 // is within (lexcially for a friend, semantically otherwise) a dependent
245 // context.
246 // FIXME: Should local extern declarations be treated like friends?
247 if (auto *AsDC = dyn_cast<DeclContext>(this))
248 return AsDC->isDependentContext();
249 auto *DC = getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
250 return DC->isDependentContext() || isTemplateDecl() || getDescribedTemplate();
251}
252
253const DeclContext *Decl::getParentFunctionOrMethod() const {
254 for (const DeclContext *DC = getDeclContext();
255 DC && !DC->isTranslationUnit() && !DC->isNamespace();
256 DC = DC->getParent())
257 if (DC->isFunctionOrMethod())
258 return DC;
259
260 return nullptr;
261}
262
263//===----------------------------------------------------------------------===//
264// PrettyStackTraceDecl Implementation
265//===----------------------------------------------------------------------===//
266
267void PrettyStackTraceDecl::print(raw_ostream &OS) const {
268 SourceLocation TheLoc = Loc;
269 if (TheLoc.isInvalid() && TheDecl)
270 TheLoc = TheDecl->getLocation();
271
272 if (TheLoc.isValid()) {
273 TheLoc.print(OS, SM);
274 OS << ": ";
275 }
276
277 OS << Message;
278
279 if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
280 OS << " '";
281 DN->printQualifiedName(OS);
282 OS << '\'';
283 }
284 OS << '\n';
285}
286
287//===----------------------------------------------------------------------===//
288// Decl Implementation
289//===----------------------------------------------------------------------===//
290
291// Out-of-line virtual method providing a home for Decl.
292Decl::~Decl() = default;
293
294void Decl::setDeclContext(DeclContext *DC) {
295 DeclCtx = DC;
296}
297
298void Decl::setLexicalDeclContext(DeclContext *DC) {
299 if (DC == getLexicalDeclContext())
300 return;
301
302 if (isInSemaDC()) {
303 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
304 } else {
305 getMultipleDC()->LexicalDC = DC;
306 }
307
308 // FIXME: We shouldn't be changing the lexical context of declarations
309 // imported from AST files.
310 if (!isFromASTFile()) {
311 setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
312 if (hasOwningModule())
313 setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
314 }
315
316 assert((((getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported
|| getOwningModule()) && "hidden declaration has no owning module"
) ? static_cast<void> (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 319, __PRETTY_FUNCTION__))
317 (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||(((getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported
|| getOwningModule()) && "hidden declaration has no owning module"
) ? static_cast<void> (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 319, __PRETTY_FUNCTION__))
318 getOwningModule()) &&(((getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported
|| getOwningModule()) && "hidden declaration has no owning module"
) ? static_cast<void> (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 319, __PRETTY_FUNCTION__))
319 "hidden declaration has no owning module")(((getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported
|| getOwningModule()) && "hidden declaration has no owning module"
) ? static_cast<void> (0) : __assert_fail ("(getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported || getOwningModule()) && \"hidden declaration has no owning module\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 319, __PRETTY_FUNCTION__))
;
320}
321
322void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
323 ASTContext &Ctx) {
324 if (SemaDC == LexicalDC) {
325 DeclCtx = SemaDC;
326 } else {
327 auto *MDC = new (Ctx) Decl::MultipleDC();
328 MDC->SemanticDC = SemaDC;
329 MDC->LexicalDC = LexicalDC;
330 DeclCtx = MDC;
331 }
332}
333
334bool Decl::isLexicallyWithinFunctionOrMethod() const {
335 const DeclContext *LDC = getLexicalDeclContext();
336 while (true) {
337 if (LDC->isFunctionOrMethod())
338 return true;
339 if (!isa<TagDecl>(LDC))
340 return false;
341 LDC = LDC->getLexicalParent();
342 }
343 return false;
344}
345
346bool Decl::isInAnonymousNamespace() const {
347 for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) {
348 if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
349 if (ND->isAnonymousNamespace())
350 return true;
351 }
352
353 return false;
354}
355
356bool Decl::isInStdNamespace() const {
357 const DeclContext *DC = getDeclContext();
358 return DC && DC->isStdNamespace();
359}
360
361TranslationUnitDecl *Decl::getTranslationUnitDecl() {
362 if (auto *TUD = dyn_cast<TranslationUnitDecl>(this))
363 return TUD;
364
365 DeclContext *DC = getDeclContext();
366 assert(DC && "This decl is not contained in a translation unit!")((DC && "This decl is not contained in a translation unit!"
) ? static_cast<void> (0) : __assert_fail ("DC && \"This decl is not contained in a translation unit!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 366, __PRETTY_FUNCTION__))
;
367
368 while (!DC->isTranslationUnit()) {
369 DC = DC->getParent();
370 assert(DC && "This decl is not contained in a translation unit!")((DC && "This decl is not contained in a translation unit!"
) ? static_cast<void> (0) : __assert_fail ("DC && \"This decl is not contained in a translation unit!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 370, __PRETTY_FUNCTION__))
;
371 }
372
373 return cast<TranslationUnitDecl>(DC);
374}
375
376ASTContext &Decl::getASTContext() const {
377 return getTranslationUnitDecl()->getASTContext();
378}
379
380ASTMutationListener *Decl::getASTMutationListener() const {
381 return getASTContext().getASTMutationListener();
382}
383
384unsigned Decl::getMaxAlignment() const {
385 if (!hasAttrs())
386 return 0;
387
388 unsigned Align = 0;
389 const AttrVec &V = getAttrs();
390 ASTContext &Ctx = getASTContext();
391 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
392 for (; I != E; ++I)
393 Align = std::max(Align, I->getAlignment(Ctx));
394 return Align;
395}
396
397bool Decl::isUsed(bool CheckUsedAttr) const {
398 const Decl *CanonD = getCanonicalDecl();
399 if (CanonD->Used)
400 return true;
401
402 // Check for used attribute.
403 // Ask the most recent decl, since attributes accumulate in the redecl chain.
404 if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
405 return true;
406
407 // The information may have not been deserialized yet. Force deserialization
408 // to complete the needed information.
409 return getMostRecentDecl()->getCanonicalDecl()->Used;
410}
411
412void Decl::markUsed(ASTContext &C) {
413 if (isUsed(false))
414 return;
415
416 if (C.getASTMutationListener())
417 C.getASTMutationListener()->DeclarationMarkedUsed(this);
418
419 setIsUsed();
420}
421
422bool Decl::isReferenced() const {
423 if (Referenced)
424 return true;
425
426 // Check redeclarations.
427 for (const auto *I : redecls())
428 if (I->Referenced)
429 return true;
430
431 return false;
432}
433
434ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
435 const Decl *Definition = nullptr;
436 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
437 Definition = ID->getDefinition();
438 } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) {
439 Definition = PD->getDefinition();
440 } else if (auto *TD = dyn_cast<TagDecl>(this)) {
441 Definition = TD->getDefinition();
442 }
443 if (!Definition)
444 Definition = this;
445
446 if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
447 return attr;
448 if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
449 return dcd->getAttr<ExternalSourceSymbolAttr>();
450 }
451
452 return nullptr;
453}
454
455bool Decl::hasDefiningAttr() const {
456 return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
457}
458
459const Attr *Decl::getDefiningAttr() const {
460 if (auto *AA = getAttr<AliasAttr>())
461 return AA;
462 if (auto *IFA = getAttr<IFuncAttr>())
463 return IFA;
464 return nullptr;
465}
466
467static StringRef getRealizedPlatform(const AvailabilityAttr *A,
468 const ASTContext &Context) {
469 // Check if this is an App Extension "platform", and if so chop off
470 // the suffix for matching with the actual platform.
471 StringRef RealizedPlatform = A->getPlatform()->getName();
472 if (!Context.getLangOpts().AppExt)
473 return RealizedPlatform;
474 size_t suffix = RealizedPlatform.rfind("_app_extension");
475 if (suffix != StringRef::npos)
476 return RealizedPlatform.slice(0, suffix);
477 return RealizedPlatform;
478}
479
480/// Determine the availability of the given declaration based on
481/// the target platform.
482///
483/// When it returns an availability result other than \c AR_Available,
484/// if the \p Message parameter is non-NULL, it will be set to a
485/// string describing why the entity is unavailable.
486///
487/// FIXME: Make these strings localizable, since they end up in
488/// diagnostics.
489static AvailabilityResult CheckAvailability(ASTContext &Context,
490 const AvailabilityAttr *A,
491 std::string *Message,
492 VersionTuple EnclosingVersion) {
493 if (EnclosingVersion.empty())
494 EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
495
496 if (EnclosingVersion.empty())
497 return AR_Available;
498
499 StringRef ActualPlatform = A->getPlatform()->getName();
500 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
501
502 // Match the platform name.
503 if (getRealizedPlatform(A, Context) != TargetPlatform)
504 return AR_Available;
505
506 StringRef PrettyPlatformName
507 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
508
509 if (PrettyPlatformName.empty())
510 PrettyPlatformName = ActualPlatform;
511
512 std::string HintMessage;
513 if (!A->getMessage().empty()) {
514 HintMessage = " - ";
515 HintMessage += A->getMessage();
516 }
517
518 // Make sure that this declaration has not been marked 'unavailable'.
519 if (A->getUnavailable()) {
520 if (Message) {
521 Message->clear();
522 llvm::raw_string_ostream Out(*Message);
523 Out << "not available on " << PrettyPlatformName
524 << HintMessage;
525 }
526
527 return AR_Unavailable;
528 }
529
530 // Make sure that this declaration has already been introduced.
531 if (!A->getIntroduced().empty() &&
532 EnclosingVersion < A->getIntroduced()) {
533 if (Message) {
534 Message->clear();
535 llvm::raw_string_ostream Out(*Message);
536 VersionTuple VTI(A->getIntroduced());
537 Out << "introduced in " << PrettyPlatformName << ' '
538 << VTI << HintMessage;
539 }
540
541 return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
542 }
543
544 // Make sure that this declaration hasn't been obsoleted.
545 if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
546 if (Message) {
547 Message->clear();
548 llvm::raw_string_ostream Out(*Message);
549 VersionTuple VTO(A->getObsoleted());
550 Out << "obsoleted in " << PrettyPlatformName << ' '
551 << VTO << HintMessage;
552 }
553
554 return AR_Unavailable;
555 }
556
557 // Make sure that this declaration hasn't been deprecated.
558 if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
559 if (Message) {
560 Message->clear();
561 llvm::raw_string_ostream Out(*Message);
562 VersionTuple VTD(A->getDeprecated());
563 Out << "first deprecated in " << PrettyPlatformName << ' '
564 << VTD << HintMessage;
565 }
566
567 return AR_Deprecated;
568 }
569
570 return AR_Available;
571}
572
573AvailabilityResult Decl::getAvailability(std::string *Message,
574 VersionTuple EnclosingVersion,
575 StringRef *RealizedPlatform) const {
576 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
577 return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
578 RealizedPlatform);
579
580 AvailabilityResult Result = AR_Available;
581 std::string ResultMessage;
582
583 for (const auto *A : attrs()) {
584 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
585 if (Result >= AR_Deprecated)
586 continue;
587
588 if (Message)
589 ResultMessage = Deprecated->getMessage();
590
591 Result = AR_Deprecated;
592 continue;
593 }
594
595 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
596 if (Message)
597 *Message = Unavailable->getMessage();
598 return AR_Unavailable;
599 }
600
601 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
602 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
603 Message, EnclosingVersion);
604
605 if (AR == AR_Unavailable) {
606 if (RealizedPlatform)
607 *RealizedPlatform = Availability->getPlatform()->getName();
608 return AR_Unavailable;
609 }
610
611 if (AR > Result) {
612 Result = AR;
613 if (Message)
614 ResultMessage.swap(*Message);
615 }
616 continue;
617 }
618 }
619
620 if (Message)
621 Message->swap(ResultMessage);
622 return Result;
623}
624
625VersionTuple Decl::getVersionIntroduced() const {
626 const ASTContext &Context = getASTContext();
627 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
628 for (const auto *A : attrs()) {
629 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
630 if (getRealizedPlatform(Availability, Context) != TargetPlatform)
631 continue;
632 if (!Availability->getIntroduced().empty())
633 return Availability->getIntroduced();
634 }
635 }
636 return {};
637}
638
639bool Decl::canBeWeakImported(bool &IsDefinition) const {
640 IsDefinition = false;
641
642 // Variables, if they aren't definitions.
643 if (const auto *Var = dyn_cast<VarDecl>(this)) {
644 if (Var->isThisDeclarationADefinition()) {
645 IsDefinition = true;
646 return false;
647 }
648 return true;
649
650 // Functions, if they aren't definitions.
651 } else if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
652 if (FD->hasBody()) {
653 IsDefinition = true;
654 return false;
655 }
656 return true;
657
658 // Objective-C classes, if this is the non-fragile runtime.
659 } else if (isa<ObjCInterfaceDecl>(this) &&
660 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
661 return true;
662
663 // Nothing else.
664 } else {
665 return false;
666 }
667}
668
669bool Decl::isWeakImported() const {
670 bool IsDefinition;
671 if (!canBeWeakImported(IsDefinition))
672 return false;
673
674 for (const auto *A : attrs()) {
675 if (isa<WeakImportAttr>(A))
676 return true;
677
678 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
679 if (CheckAvailability(getASTContext(), Availability, nullptr,
680 VersionTuple()) == AR_NotYetIntroduced)
681 return true;
682 }
683 }
684
685 return false;
686}
687
688unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
689 switch (DeclKind) {
690 case Function:
691 case CXXDeductionGuide:
692 case CXXMethod:
693 case CXXConstructor:
694 case ConstructorUsingShadow:
695 case CXXDestructor:
696 case CXXConversion:
697 case EnumConstant:
698 case Var:
699 case ImplicitParam:
700 case ParmVar:
701 case ObjCMethod:
702 case ObjCProperty:
703 case MSProperty:
704 return IDNS_Ordinary;
705 case Label:
706 return IDNS_Label;
707 case IndirectField:
708 return IDNS_Ordinary | IDNS_Member;
709
710 case Binding:
711 case NonTypeTemplateParm:
712 case VarTemplate:
713 // These (C++-only) declarations are found by redeclaration lookup for
714 // tag types, so we include them in the tag namespace.
715 return IDNS_Ordinary | IDNS_Tag;
716
717 case ObjCCompatibleAlias:
718 case ObjCInterface:
719 return IDNS_Ordinary | IDNS_Type;
720
721 case Typedef:
722 case TypeAlias:
723 case TemplateTypeParm:
724 case ObjCTypeParam:
725 return IDNS_Ordinary | IDNS_Type;
726
727 case UnresolvedUsingTypename:
728 return IDNS_Ordinary | IDNS_Type | IDNS_Using;
729
730 case UsingShadow:
731 return 0; // we'll actually overwrite this later
732
733 case UnresolvedUsingValue:
734 return IDNS_Ordinary | IDNS_Using;
735
736 case Using:
737 case UsingPack:
738 return IDNS_Using;
739
740 case ObjCProtocol:
741 return IDNS_ObjCProtocol;
742
743 case Field:
744 case ObjCAtDefsField:
745 case ObjCIvar:
746 return IDNS_Member;
747
748 case Record:
749 case CXXRecord:
750 case Enum:
751 return IDNS_Tag | IDNS_Type;
752
753 case Namespace:
754 case NamespaceAlias:
755 return IDNS_Namespace;
756
757 case FunctionTemplate:
758 return IDNS_Ordinary;
759
760 case ClassTemplate:
761 case TemplateTemplateParm:
762 case TypeAliasTemplate:
763 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
764
765 case OMPDeclareReduction:
766 return IDNS_OMPReduction;
767
768 case OMPDeclareMapper:
769 return IDNS_OMPMapper;
770
771 // Never have names.
772 case Friend:
773 case FriendTemplate:
774 case AccessSpec:
775 case LinkageSpec:
776 case Export:
777 case FileScopeAsm:
778 case StaticAssert:
779 case ObjCPropertyImpl:
780 case PragmaComment:
781 case PragmaDetectMismatch:
782 case Block:
783 case Captured:
784 case TranslationUnit:
785 case ExternCContext:
786 case Decomposition:
787
788 case UsingDirective:
789 case BuiltinTemplate:
790 case ClassTemplateSpecialization:
791 case ClassTemplatePartialSpecialization:
792 case ClassScopeFunctionSpecialization:
793 case VarTemplateSpecialization:
794 case VarTemplatePartialSpecialization:
795 case ObjCImplementation:
796 case ObjCCategory:
797 case ObjCCategoryImpl:
798 case Import:
799 case OMPThreadPrivate:
800 case OMPAllocate:
801 case OMPRequires:
802 case OMPCapturedExpr:
803 case Empty:
804 // Never looked up by name.
805 return 0;
806 }
807
808 llvm_unreachable("Invalid DeclKind!")::llvm::llvm_unreachable_internal("Invalid DeclKind!", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 808)
;
809}
810
811void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
812 assert(!HasAttrs && "Decl already contains attrs.")((!HasAttrs && "Decl already contains attrs.") ? static_cast
<void> (0) : __assert_fail ("!HasAttrs && \"Decl already contains attrs.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 812, __PRETTY_FUNCTION__))
;
813
814 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
815 assert(AttrBlank.empty() && "HasAttrs was wrong?")((AttrBlank.empty() && "HasAttrs was wrong?") ? static_cast
<void> (0) : __assert_fail ("AttrBlank.empty() && \"HasAttrs was wrong?\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 815, __PRETTY_FUNCTION__))
;
816
817 AttrBlank = attrs;
818 HasAttrs = true;
819}
820
821void Decl::dropAttrs() {
822 if (!HasAttrs) return;
823
824 HasAttrs = false;
825 getASTContext().eraseDeclAttrs(this);
826}
827
828void Decl::addAttr(Attr *A) {
829 if (!hasAttrs()) {
830 setAttrs(AttrVec(1, A));
831 return;
832 }
833
834 AttrVec &Attrs = getAttrs();
835 if (!A->isInherited()) {
836 Attrs.push_back(A);
837 return;
838 }
839
840 // Attribute inheritance is processed after attribute parsing. To keep the
841 // order as in the source code, add inherited attributes before non-inherited
842 // ones.
843 auto I = Attrs.begin(), E = Attrs.end();
844 for (; I != E; ++I) {
845 if (!(*I)->isInherited())
846 break;
847 }
848 Attrs.insert(I, A);
849}
850
851const AttrVec &Decl::getAttrs() const {
852 assert(HasAttrs && "No attrs to get!")((HasAttrs && "No attrs to get!") ? static_cast<void
> (0) : __assert_fail ("HasAttrs && \"No attrs to get!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 852, __PRETTY_FUNCTION__))
;
853 return getASTContext().getDeclAttrs(this);
854}
855
856Decl *Decl::castFromDeclContext (const DeclContext *D) {
857 Decl::Kind DK = D->getDeclKind();
858 switch(DK) {
859#define DECL(NAME, BASE)
860#define DECL_CONTEXT(NAME) \
861 case Decl::NAME: \
862 return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
863#define DECL_CONTEXT_BASE(NAME)
864#include "clang/AST/DeclNodes.inc"
865 default:
866#define DECL(NAME, BASE)
867#define DECL_CONTEXT_BASE(NAME) \
868 if (DK >= first##NAME && DK <= last##NAME) \
869 return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
870#include "clang/AST/DeclNodes.inc"
871 llvm_unreachable("a decl that inherits DeclContext isn't handled")::llvm::llvm_unreachable_internal("a decl that inherits DeclContext isn't handled"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 871)
;
872 }
873}
874
875DeclContext *Decl::castToDeclContext(const Decl *D) {
876 Decl::Kind DK = D->getKind();
877 switch(DK) {
878#define DECL(NAME, BASE)
879#define DECL_CONTEXT(NAME) \
880 case Decl::NAME: \
881 return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
882#define DECL_CONTEXT_BASE(NAME)
883#include "clang/AST/DeclNodes.inc"
884 default:
885#define DECL(NAME, BASE)
886#define DECL_CONTEXT_BASE(NAME) \
887 if (DK >= first##NAME && DK <= last##NAME) \
888 return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
889#include "clang/AST/DeclNodes.inc"
890 llvm_unreachable("a decl that inherits DeclContext isn't handled")::llvm::llvm_unreachable_internal("a decl that inherits DeclContext isn't handled"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 890)
;
891 }
892}
893
894SourceLocation Decl::getBodyRBrace() const {
895 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
896 // FunctionDecl stores EndRangeLoc for this purpose.
897 if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
898 const FunctionDecl *Definition;
899 if (FD->hasBody(Definition))
900 return Definition->getSourceRange().getEnd();
901 return {};
902 }
903
904 if (Stmt *Body = getBody())
905 return Body->getSourceRange().getEnd();
906
907 return {};
908}
909
910bool Decl::AccessDeclContextSanity() const {
911#ifndef NDEBUG
912 // Suppress this check if any of the following hold:
913 // 1. this is the translation unit (and thus has no parent)
914 // 2. this is a template parameter (and thus doesn't belong to its context)
915 // 3. this is a non-type template parameter
916 // 4. the context is not a record
917 // 5. it's invalid
918 // 6. it's a C++0x static_assert.
919 // 7. it's a block literal declaration
920 if (isa<TranslationUnitDecl>(this) ||
921 isa<TemplateTypeParmDecl>(this) ||
922 isa<NonTypeTemplateParmDecl>(this) ||
923 !isa<CXXRecordDecl>(getDeclContext()) ||
924 isInvalidDecl() ||
925 isa<StaticAssertDecl>(this) ||
926 isa<BlockDecl>(this) ||
927 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
928 // as DeclContext (?).
929 isa<ParmVarDecl>(this) ||
930 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
931 // AS_none as access specifier.
932 isa<CXXRecordDecl>(this) ||
933 isa<ClassScopeFunctionSpecializationDecl>(this))
934 return true;
935
936 assert(Access != AS_none &&((Access != AS_none && "Access specifier is AS_none inside a record decl"
) ? static_cast<void> (0) : __assert_fail ("Access != AS_none && \"Access specifier is AS_none inside a record decl\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 937, __PRETTY_FUNCTION__))
937 "Access specifier is AS_none inside a record decl")((Access != AS_none && "Access specifier is AS_none inside a record decl"
) ? static_cast<void> (0) : __assert_fail ("Access != AS_none && \"Access specifier is AS_none inside a record decl\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 937, __PRETTY_FUNCTION__))
;
938#endif
939 return true;
940}
941
942static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
943static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
944
945int64_t Decl::getID() const {
946 return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(this);
947}
948
949const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
950 QualType Ty;
951 if (const auto *D = dyn_cast<ValueDecl>(this))
952 Ty = D->getType();
953 else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
954 Ty = D->getUnderlyingType();
955 else
956 return nullptr;
957
958 if (Ty->isFunctionPointerType())
959 Ty = Ty->getAs<PointerType>()->getPointeeType();
960 else if (Ty->isFunctionReferenceType())
961 Ty = Ty->getAs<ReferenceType>()->getPointeeType();
962 else if (BlocksToo && Ty->isBlockPointerType())
963 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
964
965 return Ty->getAs<FunctionType>();
966}
967
968/// Starting at a given context (a Decl or DeclContext), look for a
969/// code context that is not a closure (a lambda, block, etc.).
970template <class T> static Decl *getNonClosureContext(T *D) {
971 if (getKind(D) == Decl::CXXMethod) {
972 auto *MD = cast<CXXMethodDecl>(D);
973 if (MD->getOverloadedOperator() == OO_Call &&
974 MD->getParent()->isLambda())
975 return getNonClosureContext(MD->getParent()->getParent());
976 return MD;
977 } else if (auto *FD = dyn_cast<FunctionDecl>(D))
978 return FD;
979 else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
980 return MD;
981 else if (auto *BD = dyn_cast<BlockDecl>(D))
982 return getNonClosureContext(BD->getParent());
983 else if (auto *CD = dyn_cast<CapturedDecl>(D))
984 return getNonClosureContext(CD->getParent());
985 else
986 return nullptr;
987}
988
989Decl *Decl::getNonClosureContext() {
990 return ::getNonClosureContext(this);
991}
992
993Decl *DeclContext::getNonClosureAncestor() {
994 return ::getNonClosureContext(this);
995}
996
997//===----------------------------------------------------------------------===//
998// DeclContext Implementation
999//===----------------------------------------------------------------------===//
1000
1001DeclContext::DeclContext(Decl::Kind K) {
1002 DeclContextBits.DeclKind = K;
1003 setHasExternalLexicalStorage(false);
1004 setHasExternalVisibleStorage(false);
1005 setNeedToReconcileExternalVisibleStorage(false);
1006 setHasLazyLocalLexicalLookups(false);
1007 setHasLazyExternalLexicalLookups(false);
1008 setUseQualifiedLookup(false);
1009}
1010
1011bool DeclContext::classof(const Decl *D) {
1012 switch (D->getKind()) {
1013#define DECL(NAME, BASE)
1014#define DECL_CONTEXT(NAME) case Decl::NAME:
1015#define DECL_CONTEXT_BASE(NAME)
1016#include "clang/AST/DeclNodes.inc"
1017 return true;
1018 default:
1019#define DECL(NAME, BASE)
1020#define DECL_CONTEXT_BASE(NAME) \
1021 if (D->getKind() >= Decl::first##NAME && \
1022 D->getKind() <= Decl::last##NAME) \
1023 return true;
1024#include "clang/AST/DeclNodes.inc"
1025 return false;
1026 }
1027}
1028
1029DeclContext::~DeclContext() = default;
1030
1031/// Find the parent context of this context that will be
1032/// used for unqualified name lookup.
1033///
1034/// Generally, the parent lookup context is the semantic context. However, for
1035/// a friend function the parent lookup context is the lexical context, which
1036/// is the class in which the friend is declared.
1037DeclContext *DeclContext::getLookupParent() {
1038 // FIXME: Find a better way to identify friends.
1039 if (isa<FunctionDecl>(this))
1040 if (getParent()->getRedeclContext()->isFileContext() &&
1041 getLexicalParent()->getRedeclContext()->isRecord())
1042 return getLexicalParent();
1043
1044 return getParent();
1045}
1046
1047const BlockDecl *DeclContext::getInnermostBlockDecl() const {
1048 const DeclContext *Ctx = this;
1049
1050 do {
1051 if (Ctx->isClosure())
1052 return cast<BlockDecl>(Ctx);
1053 Ctx = Ctx->getParent();
1054 } while (Ctx);
1055
1056 return nullptr;
1057}
1058
1059bool DeclContext::isInlineNamespace() const {
1060 return isNamespace() &&
1061 cast<NamespaceDecl>(this)->isInline();
1062}
1063
1064bool DeclContext::isStdNamespace() const {
1065 if (!isNamespace())
1066 return false;
1067
1068 const auto *ND = cast<NamespaceDecl>(this);
1069 if (ND->isInline()) {
1070 return ND->getParent()->isStdNamespace();
1071 }
1072
1073 if (!getParent()->getRedeclContext()->isTranslationUnit())
1074 return false;
1075
1076 const IdentifierInfo *II = ND->getIdentifier();
1077 return II && II->isStr("std");
1078}
1079
1080bool DeclContext::isDependentContext() const {
1081 if (isFileContext())
1082 return false;
1083
1084 if (isa<ClassTemplatePartialSpecializationDecl>(this))
1085 return true;
1086
1087 if (const auto *Record = dyn_cast<CXXRecordDecl>(this)) {
1088 if (Record->getDescribedClassTemplate())
1089 return true;
1090
1091 if (Record->isDependentLambda())
1092 return true;
1093 }
1094
1095 if (const auto *Function = dyn_cast<FunctionDecl>(this)) {
1096 if (Function->getDescribedFunctionTemplate())
1097 return true;
1098
1099 // Friend function declarations are dependent if their *lexical*
1100 // context is dependent.
1101 if (cast<Decl>(this)->getFriendObjectKind())
1102 return getLexicalParent()->isDependentContext();
1103 }
1104
1105 // FIXME: A variable template is a dependent context, but is not a
1106 // DeclContext. A context within it (such as a lambda-expression)
1107 // should be considered dependent.
1108
1109 return getParent() && getParent()->isDependentContext();
1110}
1111
1112bool DeclContext::isTransparentContext() const {
1113 if (getDeclKind() == Decl::Enum)
1114 return !cast<EnumDecl>(this)->isScoped();
1115 else if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export)
1116 return true;
1117
1118 return false;
1119}
1120
1121static bool isLinkageSpecContext(const DeclContext *DC,
1122 LinkageSpecDecl::LanguageIDs ID) {
1123 while (DC->getDeclKind() != Decl::TranslationUnit) {
1124 if (DC->getDeclKind() == Decl::LinkageSpec)
1125 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1126 DC = DC->getLexicalParent();
1127 }
1128 return false;
1129}
1130
1131bool DeclContext::isExternCContext() const {
1132 return isLinkageSpecContext(this, LinkageSpecDecl::lang_c);
1133}
1134
1135const LinkageSpecDecl *DeclContext::getExternCContext() const {
1136 const DeclContext *DC = this;
1137 while (DC->getDeclKind() != Decl::TranslationUnit) {
1138 if (DC->getDeclKind() == Decl::LinkageSpec &&
1139 cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c)
1140 return cast<LinkageSpecDecl>(DC);
1141 DC = DC->getLexicalParent();
1142 }
1143 return nullptr;
1144}
1145
1146bool DeclContext::isExternCXXContext() const {
1147 return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx);
1148}
1149
1150bool DeclContext::Encloses(const DeclContext *DC) const {
1151 if (getPrimaryContext() != this)
1152 return getPrimaryContext()->Encloses(DC);
1153
1154 for (; DC; DC = DC->getParent())
1155 if (DC->getPrimaryContext() == this)
1156 return true;
1157 return false;
1158}
1159
1160DeclContext *DeclContext::getPrimaryContext() {
1161 switch (getDeclKind()) {
1162 case Decl::TranslationUnit:
1163 case Decl::ExternCContext:
1164 case Decl::LinkageSpec:
1165 case Decl::Export:
1166 case Decl::Block:
1167 case Decl::Captured:
1168 case Decl::OMPDeclareReduction:
1169 case Decl::OMPDeclareMapper:
1170 // There is only one DeclContext for these entities.
1171 return this;
1172
1173 case Decl::Namespace:
1174 // The original namespace is our primary context.
1175 return static_cast<NamespaceDecl *>(this)->getOriginalNamespace();
1176
1177 case Decl::ObjCMethod:
1178 return this;
1179
1180 case Decl::ObjCInterface:
1181 if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this))
1182 if (auto *Def = OID->getDefinition())
1183 return Def;
1184 return this;
1185
1186 case Decl::ObjCProtocol:
1187 if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this))
1188 if (auto *Def = OPD->getDefinition())
1189 return Def;
1190 return this;
1191
1192 case Decl::ObjCCategory:
1193 return this;
1194
1195 case Decl::ObjCImplementation:
1196 case Decl::ObjCCategoryImpl:
1197 return this;
1198
1199 default:
1200 if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
1201 // If this is a tag type that has a definition or is currently
1202 // being defined, that definition is our primary context.
1203 auto *Tag = cast<TagDecl>(this);
1204
1205 if (TagDecl *Def = Tag->getDefinition())
1206 return Def;
1207
1208 if (const auto *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1209 // Note, TagType::getDecl returns the (partial) definition one exists.
1210 TagDecl *PossiblePartialDef = TagTy->getDecl();
1211 if (PossiblePartialDef->isBeingDefined())
1212 return PossiblePartialDef;
1213 } else {
1214 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()))((isa<InjectedClassNameType>(Tag->getTypeForDecl()))
? static_cast<void> (0) : __assert_fail ("isa<InjectedClassNameType>(Tag->getTypeForDecl())"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1214, __PRETTY_FUNCTION__))
;
1215 }
1216
1217 return Tag;
1218 }
1219
1220 assert(getDeclKind() >= Decl::firstFunction &&((getDeclKind() >= Decl::firstFunction && getDeclKind
() <= Decl::lastFunction && "Unknown DeclContext kind"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() >= Decl::firstFunction && getDeclKind() <= Decl::lastFunction && \"Unknown DeclContext kind\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1222, __PRETTY_FUNCTION__))
1221 getDeclKind() <= Decl::lastFunction &&((getDeclKind() >= Decl::firstFunction && getDeclKind
() <= Decl::lastFunction && "Unknown DeclContext kind"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() >= Decl::firstFunction && getDeclKind() <= Decl::lastFunction && \"Unknown DeclContext kind\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1222, __PRETTY_FUNCTION__))
1222 "Unknown DeclContext kind")((getDeclKind() >= Decl::firstFunction && getDeclKind
() <= Decl::lastFunction && "Unknown DeclContext kind"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() >= Decl::firstFunction && getDeclKind() <= Decl::lastFunction && \"Unknown DeclContext kind\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1222, __PRETTY_FUNCTION__))
;
1223 return this;
1224 }
1225}
1226
1227void
1228DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
1229 Contexts.clear();
1230
1231 if (getDeclKind() != Decl::Namespace) {
1232 Contexts.push_back(this);
1233 return;
1234 }
1235
1236 auto *Self = static_cast<NamespaceDecl *>(this);
1237 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1238 N = N->getPreviousDecl())
1239 Contexts.push_back(N);
1240
1241 std::reverse(Contexts.begin(), Contexts.end());
1242}
1243
1244std::pair<Decl *, Decl *>
1245DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls,
1246 bool FieldsAlreadyLoaded) {
1247 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1248 Decl *FirstNewDecl = nullptr;
1249 Decl *PrevDecl = nullptr;
1250 for (auto *D : Decls) {
1251 if (FieldsAlreadyLoaded && isa<FieldDecl>(D))
1252 continue;
1253
1254 if (PrevDecl)
1255 PrevDecl->NextInContextAndBits.setPointer(D);
1256 else
1257 FirstNewDecl = D;
1258
1259 PrevDecl = D;
1260 }
1261
1262 return std::make_pair(FirstNewDecl, PrevDecl);
1263}
1264
1265/// We have just acquired external visible storage, and we already have
1266/// built a lookup map. For every name in the map, pull in the new names from
1267/// the external storage.
1268void DeclContext::reconcileExternalVisibleStorage() const {
1269 assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr)((hasNeedToReconcileExternalVisibleStorage() && LookupPtr
) ? static_cast<void> (0) : __assert_fail ("hasNeedToReconcileExternalVisibleStorage() && LookupPtr"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1269, __PRETTY_FUNCTION__))
;
1270 setNeedToReconcileExternalVisibleStorage(false);
1271
1272 for (auto &Lookup : *LookupPtr)
1273 Lookup.second.setHasExternalDecls();
1274}
1275
1276/// Load the declarations within this lexical storage from an
1277/// external source.
1278/// \return \c true if any declarations were added.
1279bool
1280DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1281 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1282 assert(hasExternalLexicalStorage() && Source && "No external storage?")((hasExternalLexicalStorage() && Source && "No external storage?"
) ? static_cast<void> (0) : __assert_fail ("hasExternalLexicalStorage() && Source && \"No external storage?\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1282, __PRETTY_FUNCTION__))
;
1283
1284 // Notify that we have a DeclContext that is initializing.
1285 ExternalASTSource::Deserializing ADeclContext(Source);
1286
1287 // Load the external declarations, if any.
1288 SmallVector<Decl*, 64> Decls;
1289 setHasExternalLexicalStorage(false);
1290 Source->FindExternalLexicalDecls(this, Decls);
1291
1292 if (Decls.empty())
1293 return false;
1294
1295 // We may have already loaded just the fields of this record, in which case
1296 // we need to ignore them.
1297 bool FieldsAlreadyLoaded = false;
1298 if (const auto *RD = dyn_cast<RecordDecl>(this))
1299 FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage();
1300
1301 // Splice the newly-read declarations into the beginning of the list
1302 // of declarations.
1303 Decl *ExternalFirst, *ExternalLast;
1304 std::tie(ExternalFirst, ExternalLast) =
1305 BuildDeclChain(Decls, FieldsAlreadyLoaded);
1306 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1307 FirstDecl = ExternalFirst;
1308 if (!LastDecl)
1309 LastDecl = ExternalLast;
1310 return true;
1311}
1312
1313DeclContext::lookup_result
1314ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1315 DeclarationName Name) {
1316 ASTContext &Context = DC->getParentASTContext();
1317 StoredDeclsMap *Map;
1318 if (!(Map = DC->LookupPtr))
1319 Map = DC->CreateStoredDeclsMap(Context);
1320 if (DC->hasNeedToReconcileExternalVisibleStorage())
1321 DC->reconcileExternalVisibleStorage();
1322
1323 (*Map)[Name].removeExternalDecls();
1324
1325 return DeclContext::lookup_result();
1326}
1327
1328DeclContext::lookup_result
1329ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1330 DeclarationName Name,
1331 ArrayRef<NamedDecl*> Decls) {
1332 ASTContext &Context = DC->getParentASTContext();
1333 StoredDeclsMap *Map;
1334 if (!(Map = DC->LookupPtr))
1335 Map = DC->CreateStoredDeclsMap(Context);
1336 if (DC->hasNeedToReconcileExternalVisibleStorage())
1337 DC->reconcileExternalVisibleStorage();
1338
1339 StoredDeclsList &List = (*Map)[Name];
1340
1341 // Clear out any old external visible declarations, to avoid quadratic
1342 // performance in the redeclaration checks below.
1343 List.removeExternalDecls();
1344
1345 if (!List.isNull()) {
1346 // We have both existing declarations and new declarations for this name.
1347 // Some of the declarations may simply replace existing ones. Handle those
1348 // first.
1349 llvm::SmallVector<unsigned, 8> Skip;
1350 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1351 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
1352 Skip.push_back(I);
1353 Skip.push_back(Decls.size());
1354
1355 // Add in any new declarations.
1356 unsigned SkipPos = 0;
1357 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1358 if (I == Skip[SkipPos])
1359 ++SkipPos;
1360 else
1361 List.AddSubsequentDecl(Decls[I]);
1362 }
1363 } else {
1364 // Convert the array to a StoredDeclsList.
1365 for (auto *D : Decls) {
1366 if (List.isNull())
1367 List.setOnlyValue(D);
1368 else
1369 List.AddSubsequentDecl(D);
1370 }
1371 }
1372
1373 return List.getLookupResult();
1374}
1375
1376DeclContext::decl_iterator DeclContext::decls_begin() const {
1377 if (hasExternalLexicalStorage())
1378 LoadLexicalDeclsFromExternalStorage();
1379 return decl_iterator(FirstDecl);
1380}
1381
1382bool DeclContext::decls_empty() const {
1383 if (hasExternalLexicalStorage())
1384 LoadLexicalDeclsFromExternalStorage();
1385
1386 return !FirstDecl;
1387}
1388
1389bool DeclContext::containsDecl(Decl *D) const {
1390 return (D->getLexicalDeclContext() == this &&
1391 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1392}
1393
1394bool DeclContext::containsDeclAndLoad(Decl *D) const {
1395 if (hasExternalLexicalStorage())
1396 LoadLexicalDeclsFromExternalStorage();
1397 return containsDecl(D);
1398}
1399
1400/// shouldBeHidden - Determine whether a declaration which was declared
1401/// within its semantic context should be invisible to qualified name lookup.
1402static bool shouldBeHidden(NamedDecl *D) {
1403 // Skip unnamed declarations.
1404 if (!D->getDeclName())
1405 return true;
1406
1407 // Skip entities that can't be found by name lookup into a particular
1408 // context.
1409 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1410 D->isTemplateParameter())
1411 return true;
1412
1413 // Skip friends and local extern declarations unless they're the first
1414 // declaration of the entity.
1415 if ((D->isLocalExternDecl() || D->getFriendObjectKind()) &&
1416 D != D->getCanonicalDecl())
1417 return true;
1418
1419 // Skip template specializations.
1420 // FIXME: This feels like a hack. Should DeclarationName support
1421 // template-ids, or is there a better way to keep specializations
1422 // from being visible?
1423 if (isa<ClassTemplateSpecializationDecl>(D))
1424 return true;
1425 if (auto *FD = dyn_cast<FunctionDecl>(D))
1426 if (FD->isFunctionTemplateSpecialization())
1427 return true;
1428
1429 return false;
1430}
1431
1432void DeclContext::removeDecl(Decl *D) {
1433 assert(D->getLexicalDeclContext() == this &&((D->getLexicalDeclContext() == this && "decl being removed from non-lexical context"
) ? static_cast<void> (0) : __assert_fail ("D->getLexicalDeclContext() == this && \"decl being removed from non-lexical context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1434, __PRETTY_FUNCTION__))
1434 "decl being removed from non-lexical context")((D->getLexicalDeclContext() == this && "decl being removed from non-lexical context"
) ? static_cast<void> (0) : __assert_fail ("D->getLexicalDeclContext() == this && \"decl being removed from non-lexical context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1434, __PRETTY_FUNCTION__))
;
1435 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&(((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
"decl is not in decls list") ? static_cast<void> (0) :
__assert_fail ("(D->NextInContextAndBits.getPointer() || D == LastDecl) && \"decl is not in decls list\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1436, __PRETTY_FUNCTION__))
1436 "decl is not in decls list")(((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
"decl is not in decls list") ? static_cast<void> (0) :
__assert_fail ("(D->NextInContextAndBits.getPointer() || D == LastDecl) && \"decl is not in decls list\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1436, __PRETTY_FUNCTION__))
;
1437
1438 // Remove D from the decl chain. This is O(n) but hopefully rare.
1439 if (D == FirstDecl) {
1440 if (D == LastDecl)
1441 FirstDecl = LastDecl = nullptr;
1442 else
1443 FirstDecl = D->NextInContextAndBits.getPointer();
1444 } else {
1445 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
1446 assert(I && "decl not found in linked list")((I && "decl not found in linked list") ? static_cast
<void> (0) : __assert_fail ("I && \"decl not found in linked list\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1446, __PRETTY_FUNCTION__))
;
1447 if (I->NextInContextAndBits.getPointer() == D) {
1448 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1449 if (D == LastDecl) LastDecl = I;
1450 break;
1451 }
1452 }
1453 }
1454
1455 // Mark that D is no longer in the decl chain.
1456 D->NextInContextAndBits.setPointer(nullptr);
1457
1458 // Remove D from the lookup table if necessary.
1459 if (isa<NamedDecl>(D)) {
1460 auto *ND = cast<NamedDecl>(D);
1461
1462 // Do not try to remove the declaration if that is invisible to qualified
1463 // lookup. E.g. template specializations are skipped.
1464 if (shouldBeHidden(ND))
1465 return;
1466
1467 // Remove only decls that have a name
1468 if (!ND->getDeclName())
1469 return;
1470
1471 auto *DC = D->getDeclContext();
1472 do {
1473 StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1474 if (Map) {
1475 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1476 assert(Pos != Map->end() && "no lookup entry for decl")((Pos != Map->end() && "no lookup entry for decl")
? static_cast<void> (0) : __assert_fail ("Pos != Map->end() && \"no lookup entry for decl\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1476, __PRETTY_FUNCTION__))
;
1477 // Remove the decl only if it is contained.
1478 StoredDeclsList::DeclsTy *Vec = Pos->second.getAsVector();
1479 if ((Vec && is_contained(*Vec, ND)) || Pos->second.getAsDecl() == ND)
1480 Pos->second.remove(ND);
1481 }
1482 } while (DC->isTransparentContext() && (DC = DC->getParent()));
1483 }
1484}
1485
1486void DeclContext::addHiddenDecl(Decl *D) {
1487 assert(D->getLexicalDeclContext() == this &&((D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"
) ? static_cast<void> (0) : __assert_fail ("D->getLexicalDeclContext() == this && \"Decl inserted into wrong lexical context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1488, __PRETTY_FUNCTION__))
1488 "Decl inserted into wrong lexical context")((D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"
) ? static_cast<void> (0) : __assert_fail ("D->getLexicalDeclContext() == this && \"Decl inserted into wrong lexical context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1488, __PRETTY_FUNCTION__))
;
1489 assert(!D->getNextDeclInContext() && D != LastDecl &&((!D->getNextDeclInContext() && D != LastDecl &&
"Decl already inserted into a DeclContext") ? static_cast<
void> (0) : __assert_fail ("!D->getNextDeclInContext() && D != LastDecl && \"Decl already inserted into a DeclContext\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1490, __PRETTY_FUNCTION__))
1490 "Decl already inserted into a DeclContext")((!D->getNextDeclInContext() && D != LastDecl &&
"Decl already inserted into a DeclContext") ? static_cast<
void> (0) : __assert_fail ("!D->getNextDeclInContext() && D != LastDecl && \"Decl already inserted into a DeclContext\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1490, __PRETTY_FUNCTION__))
;
1491
1492 if (FirstDecl) {
1493 LastDecl->NextInContextAndBits.setPointer(D);
1494 LastDecl = D;
1495 } else {
1496 FirstDecl = LastDecl = D;
1497 }
1498
1499 // Notify a C++ record declaration that we've added a member, so it can
1500 // update its class-specific state.
1501 if (auto *Record = dyn_cast<CXXRecordDecl>(this))
1502 Record->addedMember(D);
1503
1504 // If this is a newly-created (not de-serialized) import declaration, wire
1505 // it in to the list of local import declarations.
1506 if (!D->isFromASTFile()) {
1507 if (auto *Import = dyn_cast<ImportDecl>(D))
1508 D->getASTContext().addedLocalImportDecl(Import);
1509 }
1510}
1511
1512void DeclContext::addDecl(Decl *D) {
1513 addHiddenDecl(D);
1514
1515 if (auto *ND = dyn_cast<NamedDecl>(D))
1516 ND->getDeclContext()->getPrimaryContext()->
1517 makeDeclVisibleInContextWithFlags(ND, false, true);
1518}
1519
1520void DeclContext::addDeclInternal(Decl *D) {
1521 addHiddenDecl(D);
1522
1523 if (auto *ND = dyn_cast<NamedDecl>(D))
1524 ND->getDeclContext()->getPrimaryContext()->
1525 makeDeclVisibleInContextWithFlags(ND, true, true);
1526}
1527
1528/// buildLookup - Build the lookup data structure with all of the
1529/// declarations in this DeclContext (and any other contexts linked
1530/// to it or transparent contexts nested within it) and return it.
1531///
1532/// Note that the produced map may miss out declarations from an
1533/// external source. If it does, those entries will be marked with
1534/// the 'hasExternalDecls' flag.
1535StoredDeclsMap *DeclContext::buildLookup() {
1536 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC")((this == getPrimaryContext() && "buildLookup called on non-primary DC"
) ? static_cast<void> (0) : __assert_fail ("this == getPrimaryContext() && \"buildLookup called on non-primary DC\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1536, __PRETTY_FUNCTION__))
;
1537
1538 if (!hasLazyLocalLexicalLookups() &&
1539 !hasLazyExternalLexicalLookups())
1540 return LookupPtr;
1541
1542 SmallVector<DeclContext *, 2> Contexts;
1543 collectAllContexts(Contexts);
1544
1545 if (hasLazyExternalLexicalLookups()) {
1546 setHasLazyExternalLexicalLookups(false);
1547 for (auto *DC : Contexts) {
1548 if (DC->hasExternalLexicalStorage()) {
1549 bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage();
1550 setHasLazyLocalLexicalLookups(
1551 hasLazyLocalLexicalLookups() | LoadedDecls );
1552 }
1553 }
1554
1555 if (!hasLazyLocalLexicalLookups())
1556 return LookupPtr;
1557 }
1558
1559 for (auto *DC : Contexts)
1560 buildLookupImpl(DC, hasExternalVisibleStorage());
1561
1562 // We no longer have any lazy decls.
1563 setHasLazyLocalLexicalLookups(false);
1564 return LookupPtr;
1565}
1566
1567/// buildLookupImpl - Build part of the lookup data structure for the
1568/// declarations contained within DCtx, which will either be this
1569/// DeclContext, a DeclContext linked to it, or a transparent context
1570/// nested within it.
1571void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1572 for (auto *D : DCtx->noload_decls()) {
1573 // Insert this declaration into the lookup structure, but only if
1574 // it's semantically within its decl context. Any other decls which
1575 // should be found in this context are added eagerly.
1576 //
1577 // If it's from an AST file, don't add it now. It'll get handled by
1578 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1579 // in C++, we do not track external visible decls for the TU, so in
1580 // that case we need to collect them all here.
1581 if (auto *ND = dyn_cast<NamedDecl>(D))
1582 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1583 (!ND->isFromASTFile() ||
1584 (isTranslationUnit() &&
1585 !getParentASTContext().getLangOpts().CPlusPlus)))
1586 makeDeclVisibleInContextImpl(ND, Internal);
1587
1588 // If this declaration is itself a transparent declaration context
1589 // or inline namespace, add the members of this declaration of that
1590 // context (recursively).
1591 if (auto *InnerCtx = dyn_cast<DeclContext>(D))
1592 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1593 buildLookupImpl(InnerCtx, Internal);
1594 }
1595}
1596
1597NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1598
1599DeclContext::lookup_result
1600DeclContext::lookup(DeclarationName Name) const {
1601 assert(getDeclKind() != Decl::LinkageSpec &&((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1603, __PRETTY_FUNCTION__))
1602 getDeclKind() != Decl::Export &&((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1603, __PRETTY_FUNCTION__))
1603 "should not perform lookups into transparent contexts")((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1603, __PRETTY_FUNCTION__))
;
1604
1605 const DeclContext *PrimaryContext = getPrimaryContext();
1606 if (PrimaryContext != this)
1607 return PrimaryContext->lookup(Name);
1608
1609 // If we have an external source, ensure that any later redeclarations of this
1610 // context have been loaded, since they may add names to the result of this
1611 // lookup (or add external visible storage).
1612 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1613 if (Source)
1614 (void)cast<Decl>(this)->getMostRecentDecl();
1615
1616 if (hasExternalVisibleStorage()) {
1617 assert(Source && "external visible storage but no external source?")((Source && "external visible storage but no external source?"
) ? static_cast<void> (0) : __assert_fail ("Source && \"external visible storage but no external source?\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1617, __PRETTY_FUNCTION__))
;
1618
1619 if (hasNeedToReconcileExternalVisibleStorage())
1620 reconcileExternalVisibleStorage();
1621
1622 StoredDeclsMap *Map = LookupPtr;
1623
1624 if (hasLazyLocalLexicalLookups() ||
1625 hasLazyExternalLexicalLookups())
1626 // FIXME: Make buildLookup const?
1627 Map = const_cast<DeclContext*>(this)->buildLookup();
1628
1629 if (!Map)
1630 Map = CreateStoredDeclsMap(getParentASTContext());
1631
1632 // If we have a lookup result with no external decls, we are done.
1633 std::pair<StoredDeclsMap::iterator, bool> R =
1634 Map->insert(std::make_pair(Name, StoredDeclsList()));
1635 if (!R.second && !R.first->second.hasExternalDecls())
1636 return R.first->second.getLookupResult();
1637
1638 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
1639 if (StoredDeclsMap *Map = LookupPtr) {
1640 StoredDeclsMap::iterator I = Map->find(Name);
1641 if (I != Map->end())
1642 return I->second.getLookupResult();
1643 }
1644 }
1645
1646 return {};
1647 }
1648
1649 StoredDeclsMap *Map = LookupPtr;
1650 if (hasLazyLocalLexicalLookups() ||
1651 hasLazyExternalLexicalLookups())
1652 Map = const_cast<DeclContext*>(this)->buildLookup();
1653
1654 if (!Map)
1655 return {};
1656
1657 StoredDeclsMap::iterator I = Map->find(Name);
1658 if (I == Map->end())
1659 return {};
1660
1661 return I->second.getLookupResult();
1662}
1663
1664DeclContext::lookup_result
1665DeclContext::noload_lookup(DeclarationName Name) {
1666 assert(getDeclKind() != Decl::LinkageSpec &&((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1668, __PRETTY_FUNCTION__))
1667 getDeclKind() != Decl::Export &&((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1668, __PRETTY_FUNCTION__))
1668 "should not perform lookups into transparent contexts")((getDeclKind() != Decl::LinkageSpec && getDeclKind()
!= Decl::Export && "should not perform lookups into transparent contexts"
) ? static_cast<void> (0) : __assert_fail ("getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export && \"should not perform lookups into transparent contexts\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1668, __PRETTY_FUNCTION__))
;
1669
1670 DeclContext *PrimaryContext = getPrimaryContext();
1671 if (PrimaryContext != this)
1672 return PrimaryContext->noload_lookup(Name);
1673
1674 loadLazyLocalLexicalLookups();
1675 StoredDeclsMap *Map = LookupPtr;
1676 if (!Map)
1677 return {};
1678
1679 StoredDeclsMap::iterator I = Map->find(Name);
1680 return I != Map->end() ? I->second.getLookupResult()
1681 : lookup_result();
1682}
1683
1684// If we have any lazy lexical declarations not in our lookup map, add them
1685// now. Don't import any external declarations, not even if we know we have
1686// some missing from the external visible lookups.
1687void DeclContext::loadLazyLocalLexicalLookups() {
1688 if (hasLazyLocalLexicalLookups()) {
1689 SmallVector<DeclContext *, 2> Contexts;
1690 collectAllContexts(Contexts);
1691 for (auto *Context : Contexts)
1692 buildLookupImpl(Context, hasExternalVisibleStorage());
1693 setHasLazyLocalLexicalLookups(false);
1694 }
1695}
1696
1697void DeclContext::localUncachedLookup(DeclarationName Name,
1698 SmallVectorImpl<NamedDecl *> &Results) {
1699 Results.clear();
1700
1701 // If there's no external storage, just perform a normal lookup and copy
1702 // the results.
1703 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1704 lookup_result LookupResults = lookup(Name);
1705 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1706 return;
1707 }
1708
1709 // If we have a lookup table, check there first. Maybe we'll get lucky.
1710 // FIXME: Should we be checking these flags on the primary context?
1711 if (Name && !hasLazyLocalLexicalLookups() &&
1712 !hasLazyExternalLexicalLookups()) {
1713 if (StoredDeclsMap *Map = LookupPtr) {
1714 StoredDeclsMap::iterator Pos = Map->find(Name);
1715 if (Pos != Map->end()) {
1716 Results.insert(Results.end(),
1717 Pos->second.getLookupResult().begin(),
1718 Pos->second.getLookupResult().end());
1719 return;
1720 }
1721 }
1722 }
1723
1724 // Slow case: grovel through the declarations in our chain looking for
1725 // matches.
1726 // FIXME: If we have lazy external declarations, this will not find them!
1727 // FIXME: Should we CollectAllContexts and walk them all here?
1728 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1729 if (auto *ND = dyn_cast<NamedDecl>(D))
1730 if (ND->getDeclName() == Name)
1731 Results.push_back(ND);
1732 }
1733}
1734
1735DeclContext *DeclContext::getRedeclContext() {
1736 DeclContext *Ctx = this;
1737
1738 // In C, a record type is the redeclaration context for its fields only. If
1739 // we arrive at a record context after skipping anything else, we should skip
1740 // the record as well. Currently, this means skipping enumerations because
1741 // they're the only transparent context that can exist within a struct or
1742 // union.
1743 bool SkipRecords = getDeclKind() == Decl::Kind::Enum &&
1744 !getParentASTContext().getLangOpts().CPlusPlus;
1745
1746 // Skip through contexts to get to the redeclaration context. Transparent
1747 // contexts are always skipped.
1748 while ((SkipRecords && Ctx->isRecord()) || Ctx->isTransparentContext())
1749 Ctx = Ctx->getParent();
1750 return Ctx;
1751}
1752
1753DeclContext *DeclContext::getEnclosingNamespaceContext() {
1754 DeclContext *Ctx = this;
1755 // Skip through non-namespace, non-translation-unit contexts.
1756 while (!Ctx->isFileContext())
1757 Ctx = Ctx->getParent();
1758 return Ctx->getPrimaryContext();
1759}
1760
1761RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1762 // Loop until we find a non-record context.
1763 RecordDecl *OutermostRD = nullptr;
1764 DeclContext *DC = this;
1765 while (DC->isRecord()) {
1766 OutermostRD = cast<RecordDecl>(DC);
1767 DC = DC->getLexicalParent();
1768 }
1769 return OutermostRD;
1770}
1771
1772bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1773 // For non-file contexts, this is equivalent to Equals.
1774 if (!isFileContext())
1775 return O->Equals(this);
1776
1777 do {
1778 if (O->Equals(this))
1779 return true;
1780
1781 const auto *NS = dyn_cast<NamespaceDecl>(O);
1782 if (!NS || !NS->isInline())
1783 break;
1784 O = NS->getParent();
1785 } while (O);
1786
1787 return false;
1788}
1789
1790void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1791 DeclContext *PrimaryDC = this->getPrimaryContext();
1792 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1793 // If the decl is being added outside of its semantic decl context, we
1794 // need to ensure that we eagerly build the lookup information for it.
1795 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1796}
1797
1798void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1799 bool Recoverable) {
1800 assert(this == getPrimaryContext() && "expected a primary DC")((this == getPrimaryContext() && "expected a primary DC"
) ? static_cast<void> (0) : __assert_fail ("this == getPrimaryContext() && \"expected a primary DC\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1800, __PRETTY_FUNCTION__))
;
1801
1802 if (!isLookupContext()) {
1803 if (isTransparentContext())
1804 getParent()->getPrimaryContext()
1805 ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1806 return;
1807 }
1808
1809 // Skip declarations which should be invisible to name lookup.
1810 if (shouldBeHidden(D))
1811 return;
1812
1813 // If we already have a lookup data structure, perform the insertion into
1814 // it. If we might have externally-stored decls with this name, look them
1815 // up and perform the insertion. If this decl was declared outside its
1816 // semantic context, buildLookup won't add it, so add it now.
1817 //
1818 // FIXME: As a performance hack, don't add such decls into the translation
1819 // unit unless we're in C++, since qualified lookup into the TU is never
1820 // performed.
1821 if (LookupPtr || hasExternalVisibleStorage() ||
1822 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1823 (getParentASTContext().getLangOpts().CPlusPlus ||
1824 !isTranslationUnit()))) {
1825 // If we have lazily omitted any decls, they might have the same name as
1826 // the decl which we are adding, so build a full lookup table before adding
1827 // this decl.
1828 buildLookup();
1829 makeDeclVisibleInContextImpl(D, Internal);
1830 } else {
1831 setHasLazyLocalLexicalLookups(true);
1832 }
1833
1834 // If we are a transparent context or inline namespace, insert into our
1835 // parent context, too. This operation is recursive.
1836 if (isTransparentContext() || isInlineNamespace())
1837 getParent()->getPrimaryContext()->
1838 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1839
1840 auto *DCAsDecl = cast<Decl>(this);
1841 // Notify that a decl was made visible unless we are a Tag being defined.
1842 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1843 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1844 L->AddedVisibleDecl(this, D);
1845}
1846
1847void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1848 // Find or create the stored declaration map.
1849 StoredDeclsMap *Map = LookupPtr;
1850 if (!Map) {
1851 ASTContext *C = &getParentASTContext();
1852 Map = CreateStoredDeclsMap(*C);
1853 }
1854
1855 // If there is an external AST source, load any declarations it knows about
1856 // with this declaration's name.
1857 // If the lookup table contains an entry about this name it means that we
1858 // have already checked the external source.
1859 if (!Internal)
1860 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1861 if (hasExternalVisibleStorage() &&
1862 Map->find(D->getDeclName()) == Map->end())
1863 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1864
1865 // Insert this declaration into the map.
1866 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1867
1868 if (Internal) {
1869 // If this is being added as part of loading an external declaration,
1870 // this may not be the only external declaration with this name.
1871 // In this case, we never try to replace an existing declaration; we'll
1872 // handle that when we finalize the list of declarations for this name.
1873 DeclNameEntries.setHasExternalDecls();
1874 DeclNameEntries.AddSubsequentDecl(D);
1875 return;
1876 }
1877
1878 if (DeclNameEntries.isNull()) {
1879 DeclNameEntries.setOnlyValue(D);
1880 return;
1881 }
1882
1883 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
1884 // This declaration has replaced an existing one for which
1885 // declarationReplaces returns true.
1886 return;
1887 }
1888
1889 // Put this declaration into the appropriate slot.
1890 DeclNameEntries.AddSubsequentDecl(D);
1891}
1892
1893UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1894 return cast<UsingDirectiveDecl>(*I);
1895}
1896
1897/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1898/// this context.
1899DeclContext::udir_range DeclContext::using_directives() const {
1900 // FIXME: Use something more efficient than normal lookup for using
1901 // directives. In C++, using directives are looked up more than anything else.
1902 lookup_result Result = lookup(UsingDirectiveDecl::getName());
1903 return udir_range(Result.begin(), Result.end());
1904}
1905
1906//===----------------------------------------------------------------------===//
1907// Creation and Destruction of StoredDeclsMaps. //
1908//===----------------------------------------------------------------------===//
1909
1910StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1911 assert(!LookupPtr && "context already has a decls map")((!LookupPtr && "context already has a decls map") ? static_cast
<void> (0) : __assert_fail ("!LookupPtr && \"context already has a decls map\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1911, __PRETTY_FUNCTION__))
;
1912 assert(getPrimaryContext() == this &&((getPrimaryContext() == this && "creating decls map on non-primary context"
) ? static_cast<void> (0) : __assert_fail ("getPrimaryContext() == this && \"creating decls map on non-primary context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1913, __PRETTY_FUNCTION__))
1913 "creating decls map on non-primary context")((getPrimaryContext() == this && "creating decls map on non-primary context"
) ? static_cast<void> (0) : __assert_fail ("getPrimaryContext() == this && \"creating decls map on non-primary context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1913, __PRETTY_FUNCTION__))
;
1914
1915 StoredDeclsMap *M;
1916 bool Dependent = isDependentContext();
1917 if (Dependent)
1918 M = new DependentStoredDeclsMap();
1919 else
1920 M = new StoredDeclsMap();
1921 M->Previous = C.LastSDM;
1922 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1923 LookupPtr = M;
1924 return M;
1925}
1926
1927void ASTContext::ReleaseDeclContextMaps() {
1928 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1929 // pointer because the subclass doesn't add anything that needs to
1930 // be deleted.
1931 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1932}
1933
1934void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1935 while (Map) {
1936 // Advance the iteration before we invalidate memory.
1937 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1938
1939 if (Dependent)
1940 delete static_cast<DependentStoredDeclsMap*>(Map);
1941 else
1942 delete Map;
1943
1944 Map = Next.getPointer();
1945 Dependent = Next.getInt();
1946 }
1947}
1948
1949DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1950 DeclContext *Parent,
1951 const PartialDiagnostic &PDiag) {
1952 assert(Parent->isDependentContext()((Parent->isDependentContext() && "cannot iterate dependent diagnostics of non-dependent context"
) ? static_cast<void> (0) : __assert_fail ("Parent->isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1953, __PRETTY_FUNCTION__))
1
Assuming the condition is true
2
'?' condition is true
1953 && "cannot iterate dependent diagnostics of non-dependent context")((Parent->isDependentContext() && "cannot iterate dependent diagnostics of non-dependent context"
) ? static_cast<void> (0) : __assert_fail ("Parent->isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/DeclBase.cpp"
, 1953, __PRETTY_FUNCTION__))
;
1954 Parent = Parent->getPrimaryContext();
1955 if (!Parent->LookupPtr)
3
Assuming the condition is false
4
Taking false branch
1956 Parent->CreateStoredDeclsMap(C);
1957
1958 auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
1959
1960 // Allocate the copy of the PartialDiagnostic via the ASTContext's
1961 // BumpPtrAllocator, rather than the ASTContext itself.
1962 PartialDiagnostic::Storage *DiagStorage = nullptr;
1963 if (PDiag.hasStorage())
5
Taking true branch
1964 DiagStorage = new (C) PartialDiagnostic::Storage;
6
Null pointer value stored to 'DiagStorage'
1965
1966 auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
7
Passing null pointer value via 2nd parameter 'Storage'
8
Calling constructor for 'DependentDiagnostic'
1967
1968 // TODO: Maybe we shouldn't reverse the order during insertion.
1969 DD->NextDiagnostic = Map->FirstDiagnostic;
1970 Map->FirstDiagnostic = DD;
1971
1972 return DD;
1973}

/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h

1//==- DependentDiagnostic.h - Dependently-generated diagnostics --*- 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 interfaces for diagnostics which may or may
10// fire based on how a template is instantiated.
11//
12// At the moment, the only consumer of this interface is access
13// control.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_AST_DEPENDENTDIAGNOSTIC_H
18#define LLVM_CLANG_AST_DEPENDENTDIAGNOSTIC_H
19
20#include "clang/AST/DeclBase.h"
21#include "clang/AST/DeclContextInternals.h"
22#include "clang/AST/Type.h"
23#include "clang/Basic/PartialDiagnostic.h"
24#include "clang/Basic/SourceLocation.h"
25#include "clang/Basic/Specifiers.h"
26#include <cassert>
27#include <iterator>
28
29namespace clang {
30
31class ASTContext;
32class CXXRecordDecl;
33class NamedDecl;
34
35/// A dependently-generated diagnostic.
36class DependentDiagnostic {
37public:
38 enum AccessNonce { Access = 0 };
39
40 static DependentDiagnostic *Create(ASTContext &Context,
41 DeclContext *Parent,
42 AccessNonce _,
43 SourceLocation Loc,
44 bool IsMemberAccess,
45 AccessSpecifier AS,
46 NamedDecl *TargetDecl,
47 CXXRecordDecl *NamingClass,
48 QualType BaseObjectType,
49 const PartialDiagnostic &PDiag) {
50 DependentDiagnostic *DD = Create(Context, Parent, PDiag);
51 DD->AccessData.Loc = Loc.getRawEncoding();
52 DD->AccessData.IsMember = IsMemberAccess;
53 DD->AccessData.Access = AS;
54 DD->AccessData.TargetDecl = TargetDecl;
55 DD->AccessData.NamingClass = NamingClass;
56 DD->AccessData.BaseObjectType = BaseObjectType.getAsOpaquePtr();
57 return DD;
58 }
59
60 unsigned getKind() const {
61 return Access;
62 }
63
64 bool isAccessToMember() const {
65 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 65, __PRETTY_FUNCTION__))
;
66 return AccessData.IsMember;
67 }
68
69 AccessSpecifier getAccess() const {
70 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 70, __PRETTY_FUNCTION__))
;
71 return AccessSpecifier(AccessData.Access);
72 }
73
74 SourceLocation getAccessLoc() const {
75 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 75, __PRETTY_FUNCTION__))
;
76 return SourceLocation::getFromRawEncoding(AccessData.Loc);
77 }
78
79 NamedDecl *getAccessTarget() const {
80 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 80, __PRETTY_FUNCTION__))
;
81 return AccessData.TargetDecl;
82 }
83
84 NamedDecl *getAccessNamingClass() const {
85 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 85, __PRETTY_FUNCTION__))
;
86 return AccessData.NamingClass;
87 }
88
89 QualType getAccessBaseObjectType() const {
90 assert(getKind() == Access)((getKind() == Access) ? static_cast<void> (0) : __assert_fail
("getKind() == Access", "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 90, __PRETTY_FUNCTION__))
;
91 return QualType::getFromOpaquePtr(AccessData.BaseObjectType);
92 }
93
94 const PartialDiagnostic &getDiagnostic() const {
95 return Diag;
96 }
97
98private:
99 friend class DeclContext::ddiag_iterator;
100 friend class DependentStoredDeclsMap;
101
102 DependentDiagnostic(const PartialDiagnostic &PDiag,
103 PartialDiagnostic::Storage *Storage)
104 : Diag(PDiag, Storage) {}
9
Passing null pointer value via 2nd parameter 'DiagStorage'
10
Calling constructor for 'PartialDiagnostic'
105
106 static DependentDiagnostic *Create(ASTContext &Context,
107 DeclContext *Parent,
108 const PartialDiagnostic &PDiag);
109
110 DependentDiagnostic *NextDiagnostic;
111
112 PartialDiagnostic Diag;
113
114 struct {
115 unsigned Loc;
116 unsigned Access : 2;
117 unsigned IsMember : 1;
118 NamedDecl *TargetDecl;
119 CXXRecordDecl *NamingClass;
120 void *BaseObjectType;
121 } AccessData;
122};
123
124/// An iterator over the dependent diagnostics in a dependent context.
125class DeclContext::ddiag_iterator {
126public:
127 ddiag_iterator() = default;
128 explicit ddiag_iterator(DependentDiagnostic *Ptr) : Ptr(Ptr) {}
129
130 using value_type = DependentDiagnostic *;
131 using reference = DependentDiagnostic *;
132 using pointer = DependentDiagnostic *;
133 using difference_type = int;
134 using iterator_category = std::forward_iterator_tag;
135
136 reference operator*() const { return Ptr; }
137
138 ddiag_iterator &operator++() {
139 assert(Ptr && "attempt to increment past end of diag list")((Ptr && "attempt to increment past end of diag list"
) ? static_cast<void> (0) : __assert_fail ("Ptr && \"attempt to increment past end of diag list\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 139, __PRETTY_FUNCTION__))
;
140 Ptr = Ptr->NextDiagnostic;
141 return *this;
142 }
143
144 ddiag_iterator operator++(int) {
145 ddiag_iterator tmp = *this;
146 ++*this;
147 return tmp;
148 }
149
150 bool operator==(ddiag_iterator Other) const {
151 return Ptr == Other.Ptr;
152 }
153
154 bool operator!=(ddiag_iterator Other) const {
155 return Ptr != Other.Ptr;
156 }
157
158 ddiag_iterator &operator+=(difference_type N) {
159 assert(N >= 0 && "cannot rewind a DeclContext::ddiag_iterator")((N >= 0 && "cannot rewind a DeclContext::ddiag_iterator"
) ? static_cast<void> (0) : __assert_fail ("N >= 0 && \"cannot rewind a DeclContext::ddiag_iterator\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 159, __PRETTY_FUNCTION__))
;
160 while (N--)
161 ++*this;
162 return *this;
163 }
164
165 ddiag_iterator operator+(difference_type N) const {
166 ddiag_iterator tmp = *this;
167 tmp += N;
168 return tmp;
169 }
170
171private:
172 DependentDiagnostic *Ptr = nullptr;
173};
174
175inline DeclContext::ddiag_range DeclContext::ddiags() const {
176 assert(isDependentContext()((isDependentContext() && "cannot iterate dependent diagnostics of non-dependent context"
) ? static_cast<void> (0) : __assert_fail ("isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 177, __PRETTY_FUNCTION__))
177 && "cannot iterate dependent diagnostics of non-dependent context")((isDependentContext() && "cannot iterate dependent diagnostics of non-dependent context"
) ? static_cast<void> (0) : __assert_fail ("isDependentContext() && \"cannot iterate dependent diagnostics of non-dependent context\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/AST/DependentDiagnostic.h"
, 177, __PRETTY_FUNCTION__))
;
178 const DependentStoredDeclsMap *Map
179 = static_cast<DependentStoredDeclsMap*>(getPrimaryContext()->getLookupPtr());
180
181 if (!Map)
182 // Return an empty range using the always-end default constructor.
183 return ddiag_range(ddiag_iterator(), ddiag_iterator());
184
185 return ddiag_range(ddiag_iterator(Map->FirstDiagnostic), ddiag_iterator());
186}
187
188} // namespace clang
189
190#endif // LLVM_CLANG_AST_DEPENDENTDIAGNOSTIC_H

/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h

1//===- PartialDiagnostic.h - Diagnostic "closures" --------------*- 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/// \file
10/// Implements a partial diagnostic that can be emitted anwyhere
11/// in a DiagnosticBuilder stream.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
16#define LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
17
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/PartialDiagnostic.h"
20#include "clang/Basic/LLVM.h"
21#include "clang/Basic/SourceLocation.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include <cassert>
25#include <cstdint>
26#include <string>
27#include <type_traits>
28#include <utility>
29
30namespace clang {
31
32class DeclContext;
33class IdentifierInfo;
34
35class PartialDiagnostic {
36public:
37 enum {
38 // The MaxArguments and MaxFixItHints member enum values from
39 // DiagnosticsEngine are private but DiagnosticsEngine declares
40 // PartialDiagnostic a friend. These enum values are redeclared
41 // here so that the nested Storage class below can access them.
42 MaxArguments = DiagnosticsEngine::MaxArguments
43 };
44
45 struct Storage {
46 enum {
47 /// The maximum number of arguments we can hold. We
48 /// currently only support up to 10 arguments (%0-%9).
49 ///
50 /// A single diagnostic with more than that almost certainly has to
51 /// be simplified anyway.
52 MaxArguments = PartialDiagnostic::MaxArguments
53 };
54
55 /// The number of entries in Arguments.
56 unsigned char NumDiagArgs = 0;
57
58 /// Specifies for each argument whether it is in DiagArgumentsStr
59 /// or in DiagArguments.
60 unsigned char DiagArgumentsKind[MaxArguments];
61
62 /// The values for the various substitution positions.
63 ///
64 /// This is used when the argument is not an std::string. The specific value
65 /// is mangled into an intptr_t and the interpretation depends on exactly
66 /// what sort of argument kind it is.
67 intptr_t DiagArgumentsVal[MaxArguments];
68
69 /// The values for the various substitution positions that have
70 /// string arguments.
71 std::string DiagArgumentsStr[MaxArguments];
72
73 /// The list of ranges added to this diagnostic.
74 SmallVector<CharSourceRange, 8> DiagRanges;
75
76 /// If valid, provides a hint with some code to insert, remove, or
77 /// modify at a particular position.
78 SmallVector<FixItHint, 6> FixItHints;
79
80 Storage() = default;
81 };
82
83 /// An allocator for Storage objects, which uses a small cache to
84 /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
85 class StorageAllocator {
86 static const unsigned NumCached = 16;
87 Storage Cached[NumCached];
88 Storage *FreeList[NumCached];
89 unsigned NumFreeListEntries;
90
91 public:
92 StorageAllocator();
93 ~StorageAllocator();
94
95 /// Allocate new storage.
96 Storage *Allocate() {
97 if (NumFreeListEntries == 0)
98 return new Storage;
99
100 Storage *Result = FreeList[--NumFreeListEntries];
101 Result->NumDiagArgs = 0;
102 Result->DiagRanges.clear();
103 Result->FixItHints.clear();
104 return Result;
105 }
106
107 /// Free the given storage object.
108 void Deallocate(Storage *S) {
109 if (S >= Cached && S <= Cached + NumCached) {
110 FreeList[NumFreeListEntries++] = S;
111 return;
112 }
113
114 delete S;
115 }
116 };
117
118private:
119 // NOTE: Sema assumes that PartialDiagnostic is location-invariant
120 // in the sense that its bits can be safely memcpy'ed and destructed
121 // in the new location.
122
123 /// The diagnostic ID.
124 mutable unsigned DiagID = 0;
125
126 /// Storage for args and ranges.
127 mutable Storage *DiagStorage = nullptr;
128
129 /// Allocator used to allocate storage for this diagnostic.
130 StorageAllocator *Allocator = nullptr;
131
132 /// Retrieve storage for this particular diagnostic.
133 Storage *getStorage() const {
134 if (DiagStorage)
135 return DiagStorage;
136
137 if (Allocator)
138 DiagStorage = Allocator->Allocate();
139 else {
140 assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))((Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t
(0))) ? static_cast<void> (0) : __assert_fail ("Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0))"
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 140, __PRETTY_FUNCTION__))
;
141 DiagStorage = new Storage;
142 }
143 return DiagStorage;
144 }
145
146 void freeStorage() {
147 if (!DiagStorage)
148 return;
149
150 // The hot path for PartialDiagnostic is when we just used it to wrap an ID
151 // (typically so we have the flexibility of passing a more complex
152 // diagnostic into the callee, but that does not commonly occur).
153 //
154 // Split this out into a slow function for silly compilers (*cough*) which
155 // can't do decent partial inlining.
156 freeStorageSlow();
157 }
158
159 void freeStorageSlow() {
160 if (Allocator)
161 Allocator->Deallocate(DiagStorage);
162 else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
163 delete DiagStorage;
164 DiagStorage = nullptr;
165 }
166
167 void AddSourceRange(const CharSourceRange &R) const {
168 if (!DiagStorage)
169 DiagStorage = getStorage();
170
171 DiagStorage->DiagRanges.push_back(R);
172 }
173
174 void AddFixItHint(const FixItHint &Hint) const {
175 if (Hint.isNull())
176 return;
177
178 if (!DiagStorage)
179 DiagStorage = getStorage();
180
181 DiagStorage->FixItHints.push_back(Hint);
182 }
183
184public:
185 struct NullDiagnostic {};
186
187 /// Create a null partial diagnostic, which cannot carry a payload,
188 /// and only exists to be swapped with a real partial diagnostic.
189 PartialDiagnostic(NullDiagnostic) {}
190
191 PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
192 : DiagID(DiagID), Allocator(&Allocator) {}
193
194 PartialDiagnostic(const PartialDiagnostic &Other)
195 : DiagID(Other.DiagID), Allocator(Other.Allocator) {
196 if (Other.DiagStorage) {
197 DiagStorage = getStorage();
198 *DiagStorage = *Other.DiagStorage;
199 }
200 }
201
202 PartialDiagnostic(PartialDiagnostic &&Other)
203 : DiagID(Other.DiagID), DiagStorage(Other.DiagStorage),
204 Allocator(Other.Allocator) {
205 Other.DiagStorage = nullptr;
206 }
207
208 PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
209 : DiagID(Other.DiagID), DiagStorage(DiagStorage),
11
Null pointer value stored to field 'DiagStorage'
210 Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0))) {
211 if (Other.DiagStorage)
12
Taking true branch
212 *this->DiagStorage = *Other.DiagStorage;
13
Called C++ object pointer is null
213 }
214
215 PartialDiagnostic(const Diagnostic &Other, StorageAllocator &Allocator)
216 : DiagID(Other.getID()), Allocator(&Allocator) {
217 // Copy arguments.
218 for (unsigned I = 0, N = Other.getNumArgs(); I != N; ++I) {
219 if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
220 AddString(Other.getArgStdStr(I));
221 else
222 AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
223 }
224
225 // Copy source ranges.
226 for (unsigned I = 0, N = Other.getNumRanges(); I != N; ++I)
227 AddSourceRange(Other.getRange(I));
228
229 // Copy fix-its.
230 for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; ++I)
231 AddFixItHint(Other.getFixItHint(I));
232 }
233
234 PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
235 DiagID = Other.DiagID;
236 if (Other.DiagStorage) {
237 if (!DiagStorage)
238 DiagStorage = getStorage();
239
240 *DiagStorage = *Other.DiagStorage;
241 } else {
242 freeStorage();
243 }
244
245 return *this;
246 }
247
248 PartialDiagnostic &operator=(PartialDiagnostic &&Other) {
249 freeStorage();
250
251 DiagID = Other.DiagID;
252 DiagStorage = Other.DiagStorage;
253 Allocator = Other.Allocator;
254
255 Other.DiagStorage = nullptr;
256 return *this;
257 }
258
259 ~PartialDiagnostic() {
260 freeStorage();
261 }
262
263 void swap(PartialDiagnostic &PD) {
264 std::swap(DiagID, PD.DiagID);
265 std::swap(DiagStorage, PD.DiagStorage);
266 std::swap(Allocator, PD.Allocator);
267 }
268
269 unsigned getDiagID() const { return DiagID; }
270
271 void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
272 if (!DiagStorage)
273 DiagStorage = getStorage();
274
275 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&((DiagStorage->NumDiagArgs < Storage::MaxArguments &&
"Too many arguments to diagnostic!") ? static_cast<void>
(0) : __assert_fail ("DiagStorage->NumDiagArgs < Storage::MaxArguments && \"Too many arguments to diagnostic!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 276, __PRETTY_FUNCTION__))
276 "Too many arguments to diagnostic!")((DiagStorage->NumDiagArgs < Storage::MaxArguments &&
"Too many arguments to diagnostic!") ? static_cast<void>
(0) : __assert_fail ("DiagStorage->NumDiagArgs < Storage::MaxArguments && \"Too many arguments to diagnostic!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 276, __PRETTY_FUNCTION__))
;
277 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
278 DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
279 }
280
281 void AddString(StringRef V) const {
282 if (!DiagStorage)
283 DiagStorage = getStorage();
284
285 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&((DiagStorage->NumDiagArgs < Storage::MaxArguments &&
"Too many arguments to diagnostic!") ? static_cast<void>
(0) : __assert_fail ("DiagStorage->NumDiagArgs < Storage::MaxArguments && \"Too many arguments to diagnostic!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 286, __PRETTY_FUNCTION__))
286 "Too many arguments to diagnostic!")((DiagStorage->NumDiagArgs < Storage::MaxArguments &&
"Too many arguments to diagnostic!") ? static_cast<void>
(0) : __assert_fail ("DiagStorage->NumDiagArgs < Storage::MaxArguments && \"Too many arguments to diagnostic!\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 286, __PRETTY_FUNCTION__))
;
287 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
288 = DiagnosticsEngine::ak_std_string;
289 DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = V;
290 }
291
292 void Emit(const DiagnosticBuilder &DB) const {
293 if (!DiagStorage)
294 return;
295
296 // Add all arguments.
297 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
298 if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
299 == DiagnosticsEngine::ak_std_string)
300 DB.AddString(DiagStorage->DiagArgumentsStr[i]);
301 else
302 DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
303 (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
304 }
305
306 // Add all ranges.
307 for (const CharSourceRange &Range : DiagStorage->DiagRanges)
308 DB.AddSourceRange(Range);
309
310 // Add all fix-its.
311 for (const FixItHint &Fix : DiagStorage->FixItHints)
312 DB.AddFixItHint(Fix);
313 }
314
315 void EmitToString(DiagnosticsEngine &Diags,
316 SmallVectorImpl<char> &Buf) const {
317 // FIXME: It should be possible to render a diagnostic to a string without
318 // messing with the state of the diagnostics engine.
319 DiagnosticBuilder DB(Diags.Report(getDiagID()));
320 Emit(DB);
321 DB.FlushCounts();
322 Diagnostic(&Diags).FormatDiagnostic(Buf);
323 DB.Clear();
324 Diags.Clear();
325 }
326
327 /// Clear out this partial diagnostic, giving it a new diagnostic ID
328 /// and removing all of its arguments, ranges, and fix-it hints.
329 void Reset(unsigned DiagID = 0) {
330 this->DiagID = DiagID;
331 freeStorage();
332 }
333
334 bool hasStorage() const { return DiagStorage != nullptr; }
335
336 /// Retrieve the string argument at the given index.
337 StringRef getStringArg(unsigned I) {
338 assert(DiagStorage && "No diagnostic storage?")((DiagStorage && "No diagnostic storage?") ? static_cast
<void> (0) : __assert_fail ("DiagStorage && \"No diagnostic storage?\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 338, __PRETTY_FUNCTION__))
;
339 assert(I < DiagStorage->NumDiagArgs && "Not enough diagnostic args")((I < DiagStorage->NumDiagArgs && "Not enough diagnostic args"
) ? static_cast<void> (0) : __assert_fail ("I < DiagStorage->NumDiagArgs && \"Not enough diagnostic args\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 339, __PRETTY_FUNCTION__))
;
340 assert(DiagStorage->DiagArgumentsKind[I]((DiagStorage->DiagArgumentsKind[I] == DiagnosticsEngine::
ak_std_string && "Not a string arg") ? static_cast<
void> (0) : __assert_fail ("DiagStorage->DiagArgumentsKind[I] == DiagnosticsEngine::ak_std_string && \"Not a string arg\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 341, __PRETTY_FUNCTION__))
341 == DiagnosticsEngine::ak_std_string && "Not a string arg")((DiagStorage->DiagArgumentsKind[I] == DiagnosticsEngine::
ak_std_string && "Not a string arg") ? static_cast<
void> (0) : __assert_fail ("DiagStorage->DiagArgumentsKind[I] == DiagnosticsEngine::ak_std_string && \"Not a string arg\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include/clang/Basic/PartialDiagnostic.h"
, 341, __PRETTY_FUNCTION__))
;
342 return DiagStorage->DiagArgumentsStr[I];
343 }
344
345 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
346 unsigned I) {
347 PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
348 return PD;
349 }
350
351 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
352 int I) {
353 PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
354 return PD;
355 }
356
357 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
358 const char *S) {
359 PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
360 DiagnosticsEngine::ak_c_string);
361 return PD;
362 }
363
364 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
365 StringRef S) {
366
367 PD.AddString(S);
368 return PD;
369 }
370
371 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
372 const IdentifierInfo *II) {
373 PD.AddTaggedVal(reinterpret_cast<intptr_t>(II),
374 DiagnosticsEngine::ak_identifierinfo);
375 return PD;
376 }
377
378 // Adds a DeclContext to the diagnostic. The enable_if template magic is here
379 // so that we only match those arguments that are (statically) DeclContexts;
380 // other arguments that derive from DeclContext (e.g., RecordDecls) will not
381 // match.
382 template<typename T>
383 friend inline
384 typename std::enable_if<std::is_same<T, DeclContext>::value,
385 const PartialDiagnostic &>::type
386 operator<<(const PartialDiagnostic &PD, T *DC) {
387 PD.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
388 DiagnosticsEngine::ak_declcontext);
389 return PD;
390 }
391
392 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
393 SourceRange R) {
394 PD.AddSourceRange(CharSourceRange::getTokenRange(R));
395 return PD;
396 }
397
398 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
399 const CharSourceRange &R) {
400 PD.AddSourceRange(R);
401 return PD;
402 }
403
404 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
405 const FixItHint &Hint) {
406 PD.AddFixItHint(Hint);
407 return PD;
408 }
409};
410
411inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
412 const PartialDiagnostic &PD) {
413 PD.Emit(DB);
414 return DB;
415}
416
417/// A partial diagnostic along with the source location where this
418/// diagnostic occurs.
419using PartialDiagnosticAt = std::pair<SourceLocation, PartialDiagnostic>;
420
421} // namespace clang
422
423#endif // LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H