Bug Summary

File:tools/clang/lib/AST/NestedNameSpecifier.cpp
Warning:line 494, column 3
Null pointer passed as an argument to a 'nonnull' parameter

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 NestedNameSpecifier.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-8/lib/clang/8.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-8~svn350071/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-8~svn350071/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn350071/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/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.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-8~svn350071/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-8~svn350071=. -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-2018-12-27-042839-1215-1 -x c++ /build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp -faddrsig

/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/include/clang/AST/NestedNameSpecifier.h

1//===- NestedNameSpecifier.h - C++ nested name specifiers -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the NestedNameSpecifier class, which represents
11// a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
16#define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
17
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/SourceLocation.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/PointerIntPair.h"
22#include "llvm/Support/Compiler.h"
23#include <cstdint>
24#include <cstdlib>
25#include <utility>
26
27namespace clang {
28
29class ASTContext;
30class CXXRecordDecl;
31class IdentifierInfo;
32class LangOptions;
33class NamespaceAliasDecl;
34class NamespaceDecl;
35struct PrintingPolicy;
36class Type;
37class TypeLoc;
38
39/// Represents a C++ nested name specifier, such as
40/// "\::std::vector<int>::".
41///
42/// C++ nested name specifiers are the prefixes to qualified
43/// names. For example, "foo::" in "foo::x" is a nested name
44/// specifier. Nested name specifiers are made up of a sequence of
45/// specifiers, each of which can be a namespace, type, identifier
46/// (for dependent names), decltype specifier, or the global specifier ('::').
47/// The last two specifiers can only appear at the start of a
48/// nested-namespace-specifier.
49class NestedNameSpecifier : public llvm::FoldingSetNode {
50 /// Enumeration describing
51 enum StoredSpecifierKind {
52 StoredIdentifier = 0,
53 StoredDecl = 1,
54 StoredTypeSpec = 2,
55 StoredTypeSpecWithTemplate = 3
56 };
57
58 /// The nested name specifier that precedes this nested name
59 /// specifier.
60 ///
61 /// The pointer is the nested-name-specifier that precedes this
62 /// one. The integer stores one of the first four values of type
63 /// SpecifierKind.
64 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
65
66 /// The last component in the nested name specifier, which
67 /// can be an identifier, a declaration, or a type.
68 ///
69 /// When the pointer is NULL, this specifier represents the global
70 /// specifier '::'. Otherwise, the pointer is one of
71 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
72 /// specifier as encoded within the prefix.
73 void* Specifier = nullptr;
74
75public:
76 /// The kind of specifier that completes this nested name
77 /// specifier.
78 enum SpecifierKind {
79 /// An identifier, stored as an IdentifierInfo*.
80 Identifier,
81
82 /// A namespace, stored as a NamespaceDecl*.
83 Namespace,
84
85 /// A namespace alias, stored as a NamespaceAliasDecl*.
86 NamespaceAlias,
87
88 /// A type, stored as a Type*.
89 TypeSpec,
90
91 /// A type that was preceded by the 'template' keyword,
92 /// stored as a Type*.
93 TypeSpecWithTemplate,
94
95 /// The global specifier '::'. There is no stored value.
96 Global,
97
98 /// Microsoft's '__super' specifier, stored as a CXXRecordDecl* of
99 /// the class it appeared in.
100 Super
101 };
102
103private:
104 /// Builds the global specifier.
105 NestedNameSpecifier() : Prefix(nullptr, StoredIdentifier) {}
106
107 /// Copy constructor used internally to clone nested name
108 /// specifiers.
109 NestedNameSpecifier(const NestedNameSpecifier &Other) = default;
110
111 /// Either find or insert the given nested name specifier
112 /// mockup in the given context.
113 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
114 const NestedNameSpecifier &Mockup);
115
116public:
117 NestedNameSpecifier &operator=(const NestedNameSpecifier &) = delete;
118
119 /// Builds a specifier combining a prefix and an identifier.
120 ///
121 /// The prefix must be dependent, since nested name specifiers
122 /// referencing an identifier are only permitted when the identifier
123 /// cannot be resolved.
124 static NestedNameSpecifier *Create(const ASTContext &Context,
125 NestedNameSpecifier *Prefix,
126 IdentifierInfo *II);
127
128 /// Builds a nested name specifier that names a namespace.
129 static NestedNameSpecifier *Create(const ASTContext &Context,
130 NestedNameSpecifier *Prefix,
131 const NamespaceDecl *NS);
132
133 /// Builds a nested name specifier that names a namespace alias.
134 static NestedNameSpecifier *Create(const ASTContext &Context,
135 NestedNameSpecifier *Prefix,
136 NamespaceAliasDecl *Alias);
137
138 /// Builds a nested name specifier that names a type.
139 static NestedNameSpecifier *Create(const ASTContext &Context,
140 NestedNameSpecifier *Prefix,
141 bool Template, const Type *T);
142
143 /// Builds a specifier that consists of just an identifier.
144 ///
145 /// The nested-name-specifier is assumed to be dependent, but has no
146 /// prefix because the prefix is implied by something outside of the
147 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
148 /// type.
149 static NestedNameSpecifier *Create(const ASTContext &Context,
150 IdentifierInfo *II);
151
152 /// Returns the nested name specifier representing the global
153 /// scope.
154 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
155
156 /// Returns the nested name specifier representing the __super scope
157 /// for the given CXXRecordDecl.
158 static NestedNameSpecifier *SuperSpecifier(const ASTContext &Context,
159 CXXRecordDecl *RD);
160
161 /// Return the prefix of this nested name specifier.
162 ///
163 /// The prefix contains all of the parts of the nested name
164 /// specifier that preced this current specifier. For example, for a
165 /// nested name specifier that represents "foo::bar::", the current
166 /// specifier will contain "bar::" and the prefix will contain
167 /// "foo::".
168 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
169
170 /// Determine what kind of nested name specifier is stored.
171 SpecifierKind getKind() const;
172
173 /// Retrieve the identifier stored in this nested name
174 /// specifier.
175 IdentifierInfo *getAsIdentifier() const {
176 if (Prefix.getInt() == StoredIdentifier)
177 return (IdentifierInfo *)Specifier;
178
179 return nullptr;
180 }
181
182 /// Retrieve the namespace stored in this nested name
183 /// specifier.
184 NamespaceDecl *getAsNamespace() const;
185
186 /// Retrieve the namespace alias stored in this nested name
187 /// specifier.
188 NamespaceAliasDecl *getAsNamespaceAlias() const;
189
190 /// Retrieve the record declaration stored in this nested name
191 /// specifier.
192 CXXRecordDecl *getAsRecordDecl() const;
193
194 /// Retrieve the type stored in this nested name specifier.
195 const Type *getAsType() const {
196 if (Prefix.getInt() == StoredTypeSpec ||
197 Prefix.getInt() == StoredTypeSpecWithTemplate)
198 return (const Type *)Specifier;
199
200 return nullptr;
201 }
202
203 /// Whether this nested name specifier refers to a dependent
204 /// type or not.
205 bool isDependent() const;
206
207 /// Whether this nested name specifier involves a template
208 /// parameter.
209 bool isInstantiationDependent() const;
210
211 /// Whether this nested-name-specifier contains an unexpanded
212 /// parameter pack (for C++11 variadic templates).
213 bool containsUnexpandedParameterPack() const;
214
215 /// Print this nested name specifier to the given output stream. If
216 /// `ResolveTemplateArguments` is true, we'll print actual types, e.g.
217 /// `ns::SomeTemplate<int, MyClass>` instead of
218 /// `ns::SomeTemplate<Container::value_type, T>`.
219 void print(raw_ostream &OS, const PrintingPolicy &Policy,
220 bool ResolveTemplateArguments = false) const;
221
222 void Profile(llvm::FoldingSetNodeID &ID) const {
223 ID.AddPointer(Prefix.getOpaqueValue());
224 ID.AddPointer(Specifier);
225 }
226
227 /// Dump the nested name specifier to standard output to aid
228 /// in debugging.
229 void dump(const LangOptions &LO) const;
230 void dump() const;
231 void dump(llvm::raw_ostream &OS) const;
232 void dump(llvm::raw_ostream &OS, const LangOptions &LO) const;
233};
234
235/// A C++ nested-name-specifier augmented with source location
236/// information.
237class NestedNameSpecifierLoc {
238 NestedNameSpecifier *Qualifier = nullptr;
239 void *Data = nullptr;
240
241 /// Determines the data length for the last component in the
242 /// given nested-name-specifier.
243 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
244
245 /// Determines the data length for the entire
246 /// nested-name-specifier.
247 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
248
249public:
250 /// Construct an empty nested-name-specifier.
251 NestedNameSpecifierLoc() = default;
252
253 /// Construct a nested-name-specifier with source location information
254 /// from
255 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
256 : Qualifier(Qualifier), Data(Data) {}
257
258 /// Evaluates true when this nested-name-specifier location is
259 /// non-empty.
260 explicit operator bool() const { return Qualifier; }
261
262 /// Evaluates true when this nested-name-specifier location is
263 /// empty.
264 bool hasQualifier() const { return Qualifier; }
265
266 /// Retrieve the nested-name-specifier to which this instance
267 /// refers.
268 NestedNameSpecifier *getNestedNameSpecifier() const {
269 return Qualifier;
270 }
271
272 /// Retrieve the opaque pointer that refers to source-location data.
273 void *getOpaqueData() const { return Data; }
274
275 /// Retrieve the source range covering the entirety of this
276 /// nested-name-specifier.
277 ///
278 /// For example, if this instance refers to a nested-name-specifier
279 /// \c \::std::vector<int>::, the returned source range would cover
280 /// from the initial '::' to the last '::'.
281 SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__));
282
283 /// Retrieve the source range covering just the last part of
284 /// this nested-name-specifier, not including the prefix.
285 ///
286 /// For example, if this instance refers to a nested-name-specifier
287 /// \c \::std::vector<int>::, the returned source range would cover
288 /// from "vector" to the last '::'.
289 SourceRange getLocalSourceRange() const;
290
291 /// Retrieve the location of the beginning of this
292 /// nested-name-specifier.
293 SourceLocation getBeginLoc() const {
294 return getSourceRange().getBegin();
295 }
296
297 /// Retrieve the location of the end of this
298 /// nested-name-specifier.
299 SourceLocation getEndLoc() const {
300 return getSourceRange().getEnd();
301 }
302
303 /// Retrieve the location of the beginning of this
304 /// component of the nested-name-specifier.
305 SourceLocation getLocalBeginLoc() const {
306 return getLocalSourceRange().getBegin();
307 }
308
309 /// Retrieve the location of the end of this component of the
310 /// nested-name-specifier.
311 SourceLocation getLocalEndLoc() const {
312 return getLocalSourceRange().getEnd();
313 }
314
315 /// Return the prefix of this nested-name-specifier.
316 ///
317 /// For example, if this instance refers to a nested-name-specifier
318 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
319 /// returned prefix may be empty, if this is the first component of
320 /// the nested-name-specifier.
321 NestedNameSpecifierLoc getPrefix() const {
322 if (!Qualifier)
323 return *this;
324
325 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
326 }
327
328 /// For a nested-name-specifier that refers to a type,
329 /// retrieve the type with source-location information.
330 TypeLoc getTypeLoc() const;
331
332 /// Determines the data length for the entire
333 /// nested-name-specifier.
334 unsigned getDataLength() const { return getDataLength(Qualifier); }
335
336 friend bool operator==(NestedNameSpecifierLoc X,
337 NestedNameSpecifierLoc Y) {
338 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
339 }
340
341 friend bool operator!=(NestedNameSpecifierLoc X,
342 NestedNameSpecifierLoc Y) {
343 return !(X == Y);
344 }
345};
346
347/// Class that aids in the construction of nested-name-specifiers along
348/// with source-location information for all of the components of the
349/// nested-name-specifier.
350class NestedNameSpecifierLocBuilder {
351 /// The current representation of the nested-name-specifier we're
352 /// building.
353 NestedNameSpecifier *Representation = nullptr;
354
355 /// Buffer used to store source-location information for the
356 /// nested-name-specifier.
357 ///
358 /// Note that we explicitly manage the buffer (rather than using a
359 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
360 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
361 char *Buffer = nullptr;
1
Null pointer value stored to field 'Buffer'
362
363 /// The size of the buffer used to store source-location information
364 /// for the nested-name-specifier.
365 unsigned BufferSize = 0;
366
367 /// The capacity of the buffer used to store source-location
368 /// information for the nested-name-specifier.
369 unsigned BufferCapacity = 0;
370
371public:
372 NestedNameSpecifierLocBuilder() = default;
373 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
374
375 NestedNameSpecifierLocBuilder &
376 operator=(const NestedNameSpecifierLocBuilder &Other);
377
378 ~NestedNameSpecifierLocBuilder() {
379 if (BufferCapacity)
380 free(Buffer);
381 }
382
383 /// Retrieve the representation of the nested-name-specifier.
384 NestedNameSpecifier *getRepresentation() const { return Representation; }
385
386 /// Extend the current nested-name-specifier by another
387 /// nested-name-specifier component of the form 'type::'.
388 ///
389 /// \param Context The AST context in which this nested-name-specifier
390 /// resides.
391 ///
392 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
393 ///
394 /// \param TL The TypeLoc that describes the type preceding the '::'.
395 ///
396 /// \param ColonColonLoc The location of the trailing '::'.
397 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
398 SourceLocation ColonColonLoc);
399
400 /// Extend the current nested-name-specifier by another
401 /// nested-name-specifier component of the form 'identifier::'.
402 ///
403 /// \param Context The AST context in which this nested-name-specifier
404 /// resides.
405 ///
406 /// \param Identifier The identifier.
407 ///
408 /// \param IdentifierLoc The location of the identifier.
409 ///
410 /// \param ColonColonLoc The location of the trailing '::'.
411 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
412 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
413
414 /// Extend the current nested-name-specifier by another
415 /// nested-name-specifier component of the form 'namespace::'.
416 ///
417 /// \param Context The AST context in which this nested-name-specifier
418 /// resides.
419 ///
420 /// \param Namespace The namespace.
421 ///
422 /// \param NamespaceLoc The location of the namespace name.
423 ///
424 /// \param ColonColonLoc The location of the trailing '::'.
425 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
426 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
427
428 /// Extend the current nested-name-specifier by another
429 /// nested-name-specifier component of the form 'namespace-alias::'.
430 ///
431 /// \param Context The AST context in which this nested-name-specifier
432 /// resides.
433 ///
434 /// \param Alias The namespace alias.
435 ///
436 /// \param AliasLoc The location of the namespace alias
437 /// name.
438 ///
439 /// \param ColonColonLoc The location of the trailing '::'.
440 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
441 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
442
443 /// Turn this (empty) nested-name-specifier into the global
444 /// nested-name-specifier '::'.
445 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
446
447 /// Turns this (empty) nested-name-specifier into '__super'
448 /// nested-name-specifier.
449 ///
450 /// \param Context The AST context in which this nested-name-specifier
451 /// resides.
452 ///
453 /// \param RD The declaration of the class in which nested-name-specifier
454 /// appeared.
455 ///
456 /// \param SuperLoc The location of the '__super' keyword.
457 /// name.
458 ///
459 /// \param ColonColonLoc The location of the trailing '::'.
460 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
461 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
462
463 /// Make a new nested-name-specifier from incomplete source-location
464 /// information.
465 ///
466 /// This routine should be used very, very rarely, in cases where we
467 /// need to synthesize a nested-name-specifier. Most code should instead use
468 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
469 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
470 SourceRange R);
471
472 /// Adopt an existing nested-name-specifier (with source-range
473 /// information).
474 void Adopt(NestedNameSpecifierLoc Other);
475
476 /// Retrieve the source range covered by this nested-name-specifier.
477 SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) {
478 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
479 }
480
481 /// Retrieve a nested-name-specifier with location information,
482 /// copied into the given AST context.
483 ///
484 /// \param Context The context into which this nested-name-specifier will be
485 /// copied.
486 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
487
488 /// Retrieve a nested-name-specifier with location
489 /// information based on the information in this builder.
490 ///
491 /// This loc will contain references to the builder's internal data and may
492 /// be invalidated by any change to the builder.
493 NestedNameSpecifierLoc getTemporary() const {
494 return NestedNameSpecifierLoc(Representation, Buffer);
495 }
496
497 /// Clear out this builder, and prepare it to build another
498 /// nested-name-specifier with source-location information.
499 void Clear() {
500 Representation = nullptr;
501 BufferSize = 0;
502 }
503
504 /// Retrieve the underlying buffer.
505 ///
506 /// \returns A pair containing a pointer to the buffer of source-location
507 /// data and the size of the source-location data that resides in that
508 /// buffer.
509 std::pair<char *, unsigned> getBuffer() const {
510 return std::make_pair(Buffer, BufferSize);
511 }
512};
513
514/// Insertion operator for diagnostics. This allows sending
515/// NestedNameSpecifiers into a diagnostic with <<.
516inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
517 NestedNameSpecifier *NNS) {
518 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
519 DiagnosticsEngine::ak_nestednamespec);
520 return DB;
521}
522
523} // namespace clang
524
525#endif // LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H

/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp

1//===- NestedNameSpecifier.cpp - C++ nested name specifiers ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the NestedNameSpecifier class, which represents
11// a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/NestedNameSpecifier.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/PrettyPrinter.h"
21#include "clang/AST/TemplateName.h"
22#include "clang/AST/Type.h"
23#include "clang/AST/TypeLoc.h"
24#include "clang/Basic/LLVM.h"
25#include "clang/Basic/LangOptions.h"
26#include "clang/Basic/SourceLocation.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include <algorithm>
34#include <cassert>
35#include <cstdlib>
36#include <cstring>
37
38using namespace clang;
39
40NestedNameSpecifier *
41NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
42 const NestedNameSpecifier &Mockup) {
43 llvm::FoldingSetNodeID ID;
44 Mockup.Profile(ID);
45
46 void *InsertPos = nullptr;
47 NestedNameSpecifier *NNS
48 = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
49 if (!NNS) {
50 NNS =
51 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
52 Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
53 }
54
55 return NNS;
56}
57
58NestedNameSpecifier *
59NestedNameSpecifier::Create(const ASTContext &Context,
60 NestedNameSpecifier *Prefix, IdentifierInfo *II) {
61 assert(II && "Identifier cannot be NULL")((II && "Identifier cannot be NULL") ? static_cast<
void> (0) : __assert_fail ("II && \"Identifier cannot be NULL\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 61, __PRETTY_FUNCTION__))
;
62 assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent")(((!Prefix || Prefix->isDependent()) && "Prefix must be dependent"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || Prefix->isDependent()) && \"Prefix must be dependent\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 62, __PRETTY_FUNCTION__))
;
63
64 NestedNameSpecifier Mockup;
65 Mockup.Prefix.setPointer(Prefix);
66 Mockup.Prefix.setInt(StoredIdentifier);
67 Mockup.Specifier = II;
68 return FindOrInsert(Context, Mockup);
69}
70
71NestedNameSpecifier *
72NestedNameSpecifier::Create(const ASTContext &Context,
73 NestedNameSpecifier *Prefix,
74 const NamespaceDecl *NS) {
75 assert(NS && "Namespace cannot be NULL")((NS && "Namespace cannot be NULL") ? static_cast<
void> (0) : __assert_fail ("NS && \"Namespace cannot be NULL\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 75, __PRETTY_FUNCTION__))
;
76 assert((!Prefix ||(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 79, __PRETTY_FUNCTION__))
77 (Prefix->getAsType() == nullptr &&(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 79, __PRETTY_FUNCTION__))
78 Prefix->getAsIdentifier() == nullptr)) &&(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 79, __PRETTY_FUNCTION__))
79 "Broken nested name specifier")(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 79, __PRETTY_FUNCTION__))
;
80 NestedNameSpecifier Mockup;
81 Mockup.Prefix.setPointer(Prefix);
82 Mockup.Prefix.setInt(StoredDecl);
83 Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
84 return FindOrInsert(Context, Mockup);
85}
86
87NestedNameSpecifier *
88NestedNameSpecifier::Create(const ASTContext &Context,
89 NestedNameSpecifier *Prefix,
90 NamespaceAliasDecl *Alias) {
91 assert(Alias && "Namespace alias cannot be NULL")((Alias && "Namespace alias cannot be NULL") ? static_cast
<void> (0) : __assert_fail ("Alias && \"Namespace alias cannot be NULL\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 91, __PRETTY_FUNCTION__))
;
92 assert((!Prefix ||(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 95, __PRETTY_FUNCTION__))
93 (Prefix->getAsType() == nullptr &&(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 95, __PRETTY_FUNCTION__))
94 Prefix->getAsIdentifier() == nullptr)) &&(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 95, __PRETTY_FUNCTION__))
95 "Broken nested name specifier")(((!Prefix || (Prefix->getAsType() == nullptr && Prefix
->getAsIdentifier() == nullptr)) && "Broken nested name specifier"
) ? static_cast<void> (0) : __assert_fail ("(!Prefix || (Prefix->getAsType() == nullptr && Prefix->getAsIdentifier() == nullptr)) && \"Broken nested name specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 95, __PRETTY_FUNCTION__))
;
96 NestedNameSpecifier Mockup;
97 Mockup.Prefix.setPointer(Prefix);
98 Mockup.Prefix.setInt(StoredDecl);
99 Mockup.Specifier = Alias;
100 return FindOrInsert(Context, Mockup);
101}
102
103NestedNameSpecifier *
104NestedNameSpecifier::Create(const ASTContext &Context,
105 NestedNameSpecifier *Prefix,
106 bool Template, const Type *T) {
107 assert(T && "Type cannot be NULL")((T && "Type cannot be NULL") ? static_cast<void>
(0) : __assert_fail ("T && \"Type cannot be NULL\"",
"/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 107, __PRETTY_FUNCTION__))
;
108 NestedNameSpecifier Mockup;
109 Mockup.Prefix.setPointer(Prefix);
110 Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
111 Mockup.Specifier = const_cast<Type*>(T);
112 return FindOrInsert(Context, Mockup);
113}
114
115NestedNameSpecifier *
116NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
117 assert(II && "Identifier cannot be NULL")((II && "Identifier cannot be NULL") ? static_cast<
void> (0) : __assert_fail ("II && \"Identifier cannot be NULL\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 117, __PRETTY_FUNCTION__))
;
118 NestedNameSpecifier Mockup;
119 Mockup.Prefix.setPointer(nullptr);
120 Mockup.Prefix.setInt(StoredIdentifier);
121 Mockup.Specifier = II;
122 return FindOrInsert(Context, Mockup);
123}
124
125NestedNameSpecifier *
126NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
127 if (!Context.GlobalNestedNameSpecifier)
128 Context.GlobalNestedNameSpecifier =
129 new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
130 return Context.GlobalNestedNameSpecifier;
131}
132
133NestedNameSpecifier *
134NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
135 CXXRecordDecl *RD) {
136 NestedNameSpecifier Mockup;
137 Mockup.Prefix.setPointer(nullptr);
138 Mockup.Prefix.setInt(StoredDecl);
139 Mockup.Specifier = RD;
140 return FindOrInsert(Context, Mockup);
141}
142
143NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
144 if (!Specifier)
145 return Global;
146
147 switch (Prefix.getInt()) {
148 case StoredIdentifier:
149 return Identifier;
150
151 case StoredDecl: {
152 NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
153 if (isa<CXXRecordDecl>(ND))
154 return Super;
155 return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias;
156 }
157
158 case StoredTypeSpec:
159 return TypeSpec;
160
161 case StoredTypeSpecWithTemplate:
162 return TypeSpecWithTemplate;
163 }
164
165 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 165)
;
166}
167
168/// Retrieve the namespace stored in this nested name specifier.
169NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
170 if (Prefix.getInt() == StoredDecl)
171 return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
172
173 return nullptr;
174}
175
176/// Retrieve the namespace alias stored in this nested name specifier.
177NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
178 if (Prefix.getInt() == StoredDecl)
179 return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
180
181 return nullptr;
182}
183
184/// Retrieve the record declaration stored in this nested name specifier.
185CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
186 switch (Prefix.getInt()) {
187 case StoredIdentifier:
188 return nullptr;
189
190 case StoredDecl:
191 return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
192
193 case StoredTypeSpec:
194 case StoredTypeSpecWithTemplate:
195 return getAsType()->getAsCXXRecordDecl();
196 }
197
198 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 198)
;
199}
200
201/// Whether this nested name specifier refers to a dependent
202/// type or not.
203bool NestedNameSpecifier::isDependent() const {
204 switch (getKind()) {
205 case Identifier:
206 // Identifier specifiers always represent dependent types
207 return true;
208
209 case Namespace:
210 case NamespaceAlias:
211 case Global:
212 return false;
213
214 case Super: {
215 CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
216 for (const auto &Base : RD->bases())
217 if (Base.getType()->isDependentType())
218 return true;
219
220 return false;
221 }
222
223 case TypeSpec:
224 case TypeSpecWithTemplate:
225 return getAsType()->isDependentType();
226 }
227
228 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 228)
;
229}
230
231/// Whether this nested name specifier refers to a dependent
232/// type or not.
233bool NestedNameSpecifier::isInstantiationDependent() const {
234 switch (getKind()) {
235 case Identifier:
236 // Identifier specifiers always represent dependent types
237 return true;
238
239 case Namespace:
240 case NamespaceAlias:
241 case Global:
242 case Super:
243 return false;
244
245 case TypeSpec:
246 case TypeSpecWithTemplate:
247 return getAsType()->isInstantiationDependentType();
248 }
249
250 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 250)
;
251}
252
253bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
254 switch (getKind()) {
255 case Identifier:
256 return getPrefix() && getPrefix()->containsUnexpandedParameterPack();
257
258 case Namespace:
259 case NamespaceAlias:
260 case Global:
261 case Super:
262 return false;
263
264 case TypeSpec:
265 case TypeSpecWithTemplate:
266 return getAsType()->containsUnexpandedParameterPack();
267 }
268
269 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 269)
;
270}
271
272/// Print this nested name specifier to the given output
273/// stream.
274void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
275 bool ResolveTemplateArguments) const {
276 if (getPrefix())
277 getPrefix()->print(OS, Policy);
278
279 switch (getKind()) {
280 case Identifier:
281 OS << getAsIdentifier()->getName();
282 break;
283
284 case Namespace:
285 if (getAsNamespace()->isAnonymousNamespace())
286 return;
287
288 OS << getAsNamespace()->getName();
289 break;
290
291 case NamespaceAlias:
292 OS << getAsNamespaceAlias()->getName();
293 break;
294
295 case Global:
296 break;
297
298 case Super:
299 OS << "__super";
300 break;
301
302 case TypeSpecWithTemplate:
303 OS << "template ";
304 // Fall through to print the type.
305 LLVM_FALLTHROUGH[[clang::fallthrough]];
306
307 case TypeSpec: {
308 const auto *Record =
309 dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
310 if (ResolveTemplateArguments && Record) {
311 // Print the type trait with resolved template parameters.
312 Record->printName(OS);
313 printTemplateArgumentList(OS, Record->getTemplateArgs().asArray(),
314 Policy);
315 break;
316 }
317 const Type *T = getAsType();
318
319 PrintingPolicy InnerPolicy(Policy);
320 InnerPolicy.SuppressScope = true;
321
322 // Nested-name-specifiers are intended to contain minimally-qualified
323 // types. An actual ElaboratedType will not occur, since we'll store
324 // just the type that is referred to in the nested-name-specifier (e.g.,
325 // a TypedefType, TagType, etc.). However, when we are dealing with
326 // dependent template-id types (e.g., Outer<T>::template Inner<U>),
327 // the type requires its own nested-name-specifier for uniqueness, so we
328 // suppress that nested-name-specifier during printing.
329 assert(!isa<ElaboratedType>(T) &&((!isa<ElaboratedType>(T) && "Elaborated type in nested-name-specifier"
) ? static_cast<void> (0) : __assert_fail ("!isa<ElaboratedType>(T) && \"Elaborated type in nested-name-specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 330, __PRETTY_FUNCTION__))
330 "Elaborated type in nested-name-specifier")((!isa<ElaboratedType>(T) && "Elaborated type in nested-name-specifier"
) ? static_cast<void> (0) : __assert_fail ("!isa<ElaboratedType>(T) && \"Elaborated type in nested-name-specifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 330, __PRETTY_FUNCTION__))
;
331 if (const TemplateSpecializationType *SpecType
332 = dyn_cast<TemplateSpecializationType>(T)) {
333 // Print the template name without its corresponding
334 // nested-name-specifier.
335 SpecType->getTemplateName().print(OS, InnerPolicy, true);
336
337 // Print the template argument list.
338 printTemplateArgumentList(OS, SpecType->template_arguments(),
339 InnerPolicy);
340 } else {
341 // Print the type normally
342 QualType(T, 0).print(OS, InnerPolicy);
343 }
344 break;
345 }
346 }
347
348 OS << "::";
349}
350
351LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void NestedNameSpecifier::dump(const LangOptions &LO) const {
352 dump(llvm::errs(), LO);
353}
354
355LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void NestedNameSpecifier::dump() const { dump(llvm::errs()); }
356
357LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
358 LangOptions LO;
359 dump(OS, LO);
360}
361
362LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
363 const LangOptions &LO) const {
364 print(OS, PrintingPolicy(LO));
365}
366
367unsigned
368NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
369 assert(Qualifier && "Expected a non-NULL qualifier")((Qualifier && "Expected a non-NULL qualifier") ? static_cast
<void> (0) : __assert_fail ("Qualifier && \"Expected a non-NULL qualifier\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 369, __PRETTY_FUNCTION__))
;
370
371 // Location of the trailing '::'.
372 unsigned Length = sizeof(unsigned);
373
374 switch (Qualifier->getKind()) {
375 case NestedNameSpecifier::Global:
376 // Nothing more to add.
377 break;
378
379 case NestedNameSpecifier::Identifier:
380 case NestedNameSpecifier::Namespace:
381 case NestedNameSpecifier::NamespaceAlias:
382 case NestedNameSpecifier::Super:
383 // The location of the identifier or namespace name.
384 Length += sizeof(unsigned);
385 break;
386
387 case NestedNameSpecifier::TypeSpecWithTemplate:
388 case NestedNameSpecifier::TypeSpec:
389 // The "void*" that points at the TypeLoc data.
390 // Note: the 'template' keyword is part of the TypeLoc.
391 Length += sizeof(void *);
392 break;
393 }
394
395 return Length;
396}
397
398unsigned
399NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
400 unsigned Length = 0;
401 for (; Qualifier; Qualifier = Qualifier->getPrefix())
402 Length += getLocalDataLength(Qualifier);
403 return Length;
404}
405
406/// Load a (possibly unaligned) source location from a given address
407/// and offset.
408static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
409 unsigned Raw;
410 memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned));
411 return SourceLocation::getFromRawEncoding(Raw);
412}
413
414/// Load a (possibly unaligned) pointer from a given address and
415/// offset.
416static void *LoadPointer(void *Data, unsigned Offset) {
417 void *Result;
418 memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
419 return Result;
420}
421
422SourceRange NestedNameSpecifierLoc::getSourceRange() const {
423 if (!Qualifier)
424 return SourceRange();
425
426 NestedNameSpecifierLoc First = *this;
427 while (NestedNameSpecifierLoc Prefix = First.getPrefix())
428 First = Prefix;
429
430 return SourceRange(First.getLocalSourceRange().getBegin(),
431 getLocalSourceRange().getEnd());
432}
433
434SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
435 if (!Qualifier)
436 return SourceRange();
437
438 unsigned Offset = getDataLength(Qualifier->getPrefix());
439 switch (Qualifier->getKind()) {
440 case NestedNameSpecifier::Global:
441 return LoadSourceLocation(Data, Offset);
442
443 case NestedNameSpecifier::Identifier:
444 case NestedNameSpecifier::Namespace:
445 case NestedNameSpecifier::NamespaceAlias:
446 case NestedNameSpecifier::Super:
447 return SourceRange(LoadSourceLocation(Data, Offset),
448 LoadSourceLocation(Data, Offset + sizeof(unsigned)));
449
450 case NestedNameSpecifier::TypeSpecWithTemplate:
451 case NestedNameSpecifier::TypeSpec: {
452 // The "void*" that points at the TypeLoc data.
453 // Note: the 'template' keyword is part of the TypeLoc.
454 void *TypeData = LoadPointer(Data, Offset);
455 TypeLoc TL(Qualifier->getAsType(), TypeData);
456 return SourceRange(TL.getBeginLoc(),
457 LoadSourceLocation(Data, Offset + sizeof(void*)));
458 }
459 }
460
461 llvm_unreachable("Invalid NNS Kind!")::llvm::llvm_unreachable_internal("Invalid NNS Kind!", "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 461)
;
462}
463
464TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
465 assert((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||(((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate
) && "Nested-name-specifier location is not a type") ?
static_cast<void> (0) : __assert_fail ("(Qualifier->getKind() == NestedNameSpecifier::TypeSpec || Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) && \"Nested-name-specifier location is not a type\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 467, __PRETTY_FUNCTION__))
466 Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) &&(((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate
) && "Nested-name-specifier location is not a type") ?
static_cast<void> (0) : __assert_fail ("(Qualifier->getKind() == NestedNameSpecifier::TypeSpec || Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) && \"Nested-name-specifier location is not a type\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 467, __PRETTY_FUNCTION__))
467 "Nested-name-specifier location is not a type")(((Qualifier->getKind() == NestedNameSpecifier::TypeSpec ||
Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate
) && "Nested-name-specifier location is not a type") ?
static_cast<void> (0) : __assert_fail ("(Qualifier->getKind() == NestedNameSpecifier::TypeSpec || Qualifier->getKind() == NestedNameSpecifier::TypeSpecWithTemplate) && \"Nested-name-specifier location is not a type\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 467, __PRETTY_FUNCTION__))
;
468
469 // The "void*" that points at the TypeLoc data.
470 unsigned Offset = getDataLength(Qualifier->getPrefix());
471 void *TypeData = LoadPointer(Data, Offset);
472 return TypeLoc(Qualifier->getAsType(), TypeData);
473}
474
475static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
476 unsigned &BufferCapacity) {
477 if (Start == End)
8
Taking false branch
478 return;
479
480 if (BufferSize + (End - Start) > BufferCapacity) {
9
Taking false branch
481 // Reallocate the buffer.
482 unsigned NewCapacity = std::max(
483 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
484 (unsigned)(BufferSize + (End - Start)));
485 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
486 if (BufferCapacity) {
487 memcpy(NewBuffer, Buffer, BufferSize);
488 free(Buffer);
489 }
490 Buffer = NewBuffer;
491 BufferCapacity = NewCapacity;
492 }
493
494 memcpy(Buffer + BufferSize, Start, End - Start);
10
Null pointer passed as an argument to a 'nonnull' parameter
495 BufferSize += End-Start;
496}
497
498/// Save a source location to the given buffer.
499static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
500 unsigned &BufferSize, unsigned &BufferCapacity) {
501 unsigned Raw = Loc.getRawEncoding();
502 Append(reinterpret_cast<char *>(&Raw),
503 reinterpret_cast<char *>(&Raw) + sizeof(unsigned),
504 Buffer, BufferSize, BufferCapacity);
505}
506
507/// Save a pointer to the given buffer.
508static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
509 unsigned &BufferCapacity) {
510 Append(reinterpret_cast<char *>(&Ptr),
511 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
512 Buffer, BufferSize, BufferCapacity);
513}
514
515NestedNameSpecifierLocBuilder::
516NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
517 : Representation(Other.Representation) {
518 if (!Other.Buffer)
2
Assuming the condition is false
3
Taking false branch
519 return;
520
521 if (Other.BufferCapacity == 0) {
4
Assuming the condition is false
5
Taking false branch
522 // Shallow copy is okay.
523 Buffer = Other.Buffer;
524 BufferSize = Other.BufferSize;
525 return;
526 }
527
528 // Deep copy
529 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
6
Passing value via 3rd parameter 'Buffer'
7
Calling 'Append'
530 BufferCapacity);
531}
532
533NestedNameSpecifierLocBuilder &
534NestedNameSpecifierLocBuilder::
535operator=(const NestedNameSpecifierLocBuilder &Other) {
536 Representation = Other.Representation;
537
538 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
539 // Re-use our storage.
540 BufferSize = Other.BufferSize;
541 memcpy(Buffer, Other.Buffer, BufferSize);
542 return *this;
543 }
544
545 // Free our storage, if we have any.
546 if (BufferCapacity) {
547 free(Buffer);
548 BufferCapacity = 0;
549 }
550
551 if (!Other.Buffer) {
552 // Empty.
553 Buffer = nullptr;
554 BufferSize = 0;
555 return *this;
556 }
557
558 if (Other.BufferCapacity == 0) {
559 // Shallow copy is okay.
560 Buffer = Other.Buffer;
561 BufferSize = Other.BufferSize;
562 return *this;
563 }
564
565 // Deep copy.
566 BufferSize = 0;
567 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
568 BufferCapacity);
569 return *this;
570}
571
572void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
573 SourceLocation TemplateKWLoc,
574 TypeLoc TL,
575 SourceLocation ColonColonLoc) {
576 Representation = NestedNameSpecifier::Create(Context, Representation,
577 TemplateKWLoc.isValid(),
578 TL.getTypePtr());
579
580 // Push source-location info into the buffer.
581 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
582 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
583}
584
585void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
586 IdentifierInfo *Identifier,
587 SourceLocation IdentifierLoc,
588 SourceLocation ColonColonLoc) {
589 Representation = NestedNameSpecifier::Create(Context, Representation,
590 Identifier);
591
592 // Push source-location info into the buffer.
593 SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
594 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
595}
596
597void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
598 NamespaceDecl *Namespace,
599 SourceLocation NamespaceLoc,
600 SourceLocation ColonColonLoc) {
601 Representation = NestedNameSpecifier::Create(Context, Representation,
602 Namespace);
603
604 // Push source-location info into the buffer.
605 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
606 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
607}
608
609void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
610 NamespaceAliasDecl *Alias,
611 SourceLocation AliasLoc,
612 SourceLocation ColonColonLoc) {
613 Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
614
615 // Push source-location info into the buffer.
616 SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
617 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
618}
619
620void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
621 SourceLocation ColonColonLoc) {
622 assert(!Representation && "Already have a nested-name-specifier!?")((!Representation && "Already have a nested-name-specifier!?"
) ? static_cast<void> (0) : __assert_fail ("!Representation && \"Already have a nested-name-specifier!?\""
, "/build/llvm-toolchain-snapshot-8~svn350071/tools/clang/lib/AST/NestedNameSpecifier.cpp"
, 622, __PRETTY_FUNCTION__))
;
623 Representation = NestedNameSpecifier::GlobalSpecifier(Context);
624
625 // Push source-location info into the buffer.
626 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
627}
628
629void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
630 CXXRecordDecl *RD,
631 SourceLocation SuperLoc,
632 SourceLocation ColonColonLoc) {
633 Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
634
635 // Push source-location info into the buffer.
636 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
637 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
638}
639
640void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
641 NestedNameSpecifier *Qualifier,
642 SourceRange R) {
643 Representation = Qualifier;
644
645 // Construct bogus (but well-formed) source information for the
646 // nested-name-specifier.
647 BufferSize = 0;
648 SmallVector<NestedNameSpecifier *, 4> Stack;
649 for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
650 Stack.push_back(NNS);
651 while (!Stack.empty()) {
652 NestedNameSpecifier *NNS = Stack.pop_back_val();
653 switch (NNS->getKind()) {
654 case NestedNameSpecifier::Identifier:
655 case NestedNameSpecifier::Namespace:
656 case NestedNameSpecifier::NamespaceAlias:
657 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
658 break;
659
660 case NestedNameSpecifier::TypeSpec:
661 case NestedNameSpecifier::TypeSpecWithTemplate: {
662 TypeSourceInfo *TSInfo
663 = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
664 R.getBegin());
665 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
666 BufferCapacity);
667 break;
668 }
669
670 case NestedNameSpecifier::Global:
671 case NestedNameSpecifier::Super:
672 break;
673 }
674
675 // Save the location of the '::'.
676 SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
677 Buffer, BufferSize, BufferCapacity);
678 }
679}
680
681void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
682 if (BufferCapacity)
683 free(Buffer);
684
685 if (!Other) {
686 Representation = nullptr;
687 BufferSize = 0;
688 return;
689 }
690
691 // Rather than copying the data (which is wasteful), "adopt" the
692 // pointer (which points into the ASTContext) but set the capacity to zero to
693 // indicate that we don't own it.
694 Representation = Other.getNestedNameSpecifier();
695 Buffer = static_cast<char *>(Other.getOpaqueData());
696 BufferSize = Other.getDataLength();
697 BufferCapacity = 0;
698}
699
700NestedNameSpecifierLoc
701NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
702 if (!Representation)
703 return NestedNameSpecifierLoc();
704
705 // If we adopted our data pointer from elsewhere in the AST context, there's
706 // no need to copy the memory.
707 if (BufferCapacity == 0)
708 return NestedNameSpecifierLoc(Representation, Buffer);
709
710 // FIXME: After copying the source-location information, should we free
711 // our (temporary) buffer and adopt the ASTContext-allocated memory?
712 // Doing so would optimize repeated calls to getWithLocInContext().
713 void *Mem = Context.Allocate(BufferSize, alignof(void *));
714 memcpy(Mem, Buffer, BufferSize);
715 return NestedNameSpecifierLoc(Representation, Mem);
716}