Bug Summary

File:tools/clang/lib/AST/NestedNameSpecifier.cpp
Warning:line 478, 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-eagerly-assume -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 -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-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn338205/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn338205/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn338205/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/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-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/clang/lib/AST -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -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-07-29-043837-17923-1 -x c++ /build/llvm-toolchain-snapshot-7~svn338205/tools/clang/lib/AST/NestedNameSpecifier.cpp -faddrsig

/build/llvm-toolchain-snapshot-7~svn338205/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
216 /// stream.
217 void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
218
219 void Profile(llvm::FoldingSetNodeID &ID) const {
220 ID.AddPointer(Prefix.getOpaqueValue());
221 ID.AddPointer(Specifier);
222 }
223
224 /// Dump the nested name specifier to standard output to aid
225 /// in debugging.
226 void dump(const LangOptions &LO) const;
227 void dump() const;
228};
229
230/// A C++ nested-name-specifier augmented with source location
231/// information.
232class NestedNameSpecifierLoc {
233 NestedNameSpecifier *Qualifier = nullptr;
234 void *Data = nullptr;
235
236 /// Determines the data length for the last component in the
237 /// given nested-name-specifier.
238 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
239
240 /// Determines the data length for the entire
241 /// nested-name-specifier.
242 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
243
244public:
245 /// Construct an empty nested-name-specifier.
246 NestedNameSpecifierLoc() = default;
247
248 /// Construct a nested-name-specifier with source location information
249 /// from
250 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
251 : Qualifier(Qualifier), Data(Data) {}
252
253 /// Evaluates true when this nested-name-specifier location is
254 /// non-empty.
255 explicit operator bool() const { return Qualifier; }
256
257 /// Evaluates true when this nested-name-specifier location is
258 /// empty.
259 bool hasQualifier() const { return Qualifier; }
260
261 /// Retrieve the nested-name-specifier to which this instance
262 /// refers.
263 NestedNameSpecifier *getNestedNameSpecifier() const {
264 return Qualifier;
265 }
266
267 /// Retrieve the opaque pointer that refers to source-location data.
268 void *getOpaqueData() const { return Data; }
269
270 /// Retrieve the source range covering the entirety of this
271 /// nested-name-specifier.
272 ///
273 /// For example, if this instance refers to a nested-name-specifier
274 /// \c \::std::vector<int>::, the returned source range would cover
275 /// from the initial '::' to the last '::'.
276 SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__));
277
278 /// Retrieve the source range covering just the last part of
279 /// this nested-name-specifier, not including the prefix.
280 ///
281 /// For example, if this instance refers to a nested-name-specifier
282 /// \c \::std::vector<int>::, the returned source range would cover
283 /// from "vector" to the last '::'.
284 SourceRange getLocalSourceRange() const;
285
286 /// Retrieve the location of the beginning of this
287 /// nested-name-specifier.
288 SourceLocation getBeginLoc() const {
289 return getSourceRange().getBegin();
290 }
291
292 /// Retrieve the location of the end of this
293 /// nested-name-specifier.
294 SourceLocation getEndLoc() const {
295 return getSourceRange().getEnd();
296 }
297
298 /// Retrieve the location of the beginning of this
299 /// component of the nested-name-specifier.
300 SourceLocation getLocalBeginLoc() const {
301 return getLocalSourceRange().getBegin();
302 }
303
304 /// Retrieve the location of the end of this component of the
305 /// nested-name-specifier.
306 SourceLocation getLocalEndLoc() const {
307 return getLocalSourceRange().getEnd();
308 }
309
310 /// Return the prefix of this nested-name-specifier.
311 ///
312 /// For example, if this instance refers to a nested-name-specifier
313 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
314 /// returned prefix may be empty, if this is the first component of
315 /// the nested-name-specifier.
316 NestedNameSpecifierLoc getPrefix() const {
317 if (!Qualifier)
318 return *this;
319
320 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
321 }
322
323 /// For a nested-name-specifier that refers to a type,
324 /// retrieve the type with source-location information.
325 TypeLoc getTypeLoc() const;
326
327 /// Determines the data length for the entire
328 /// nested-name-specifier.
329 unsigned getDataLength() const { return getDataLength(Qualifier); }
330
331 friend bool operator==(NestedNameSpecifierLoc X,
332 NestedNameSpecifierLoc Y) {
333 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
334 }
335
336 friend bool operator!=(NestedNameSpecifierLoc X,
337 NestedNameSpecifierLoc Y) {
338 return !(X == Y);
339 }
340};
341
342/// Class that aids in the construction of nested-name-specifiers along
343/// with source-location information for all of the components of the
344/// nested-name-specifier.
345class NestedNameSpecifierLocBuilder {
346 /// The current representation of the nested-name-specifier we're
347 /// building.
348 NestedNameSpecifier *Representation = nullptr;
349
350 /// Buffer used to store source-location information for the
351 /// nested-name-specifier.
352 ///
353 /// Note that we explicitly manage the buffer (rather than using a
354 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
355 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
356 char *Buffer = nullptr;
1
Null pointer value stored to field 'Buffer'
357
358 /// The size of the buffer used to store source-location information
359 /// for the nested-name-specifier.
360 unsigned BufferSize = 0;
361
362 /// The capacity of the buffer used to store source-location
363 /// information for the nested-name-specifier.
364 unsigned BufferCapacity = 0;
365
366public:
367 NestedNameSpecifierLocBuilder() = default;
368 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
369
370 NestedNameSpecifierLocBuilder &
371 operator=(const NestedNameSpecifierLocBuilder &Other);
372
373 ~NestedNameSpecifierLocBuilder() {
374 if (BufferCapacity)
375 free(Buffer);
376 }
377
378 /// Retrieve the representation of the nested-name-specifier.
379 NestedNameSpecifier *getRepresentation() const { return Representation; }
380
381 /// Extend the current nested-name-specifier by another
382 /// nested-name-specifier component of the form 'type::'.
383 ///
384 /// \param Context The AST context in which this nested-name-specifier
385 /// resides.
386 ///
387 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
388 ///
389 /// \param TL The TypeLoc that describes the type preceding the '::'.
390 ///
391 /// \param ColonColonLoc The location of the trailing '::'.
392 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
393 SourceLocation ColonColonLoc);
394
395 /// Extend the current nested-name-specifier by another
396 /// nested-name-specifier component of the form 'identifier::'.
397 ///
398 /// \param Context The AST context in which this nested-name-specifier
399 /// resides.
400 ///
401 /// \param Identifier The identifier.
402 ///
403 /// \param IdentifierLoc The location of the identifier.
404 ///
405 /// \param ColonColonLoc The location of the trailing '::'.
406 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
407 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
408
409 /// Extend the current nested-name-specifier by another
410 /// nested-name-specifier component of the form 'namespace::'.
411 ///
412 /// \param Context The AST context in which this nested-name-specifier
413 /// resides.
414 ///
415 /// \param Namespace The namespace.
416 ///
417 /// \param NamespaceLoc The location of the namespace name.
418 ///
419 /// \param ColonColonLoc The location of the trailing '::'.
420 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
421 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
422
423 /// Extend the current nested-name-specifier by another
424 /// nested-name-specifier component of the form 'namespace-alias::'.
425 ///
426 /// \param Context The AST context in which this nested-name-specifier
427 /// resides.
428 ///
429 /// \param Alias The namespace alias.
430 ///
431 /// \param AliasLoc The location of the namespace alias
432 /// name.
433 ///
434 /// \param ColonColonLoc The location of the trailing '::'.
435 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
436 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
437
438 /// Turn this (empty) nested-name-specifier into the global
439 /// nested-name-specifier '::'.
440 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
441
442 /// Turns this (empty) nested-name-specifier into '__super'
443 /// nested-name-specifier.
444 ///
445 /// \param Context The AST context in which this nested-name-specifier
446 /// resides.
447 ///
448 /// \param RD The declaration of the class in which nested-name-specifier
449 /// appeared.
450 ///
451 /// \param SuperLoc The location of the '__super' keyword.
452 /// name.
453 ///
454 /// \param ColonColonLoc The location of the trailing '::'.
455 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
456 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
457
458 /// Make a new nested-name-specifier from incomplete source-location
459 /// information.
460 ///
461 /// This routine should be used very, very rarely, in cases where we
462 /// need to synthesize a nested-name-specifier. Most code should instead use
463 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
464 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
465 SourceRange R);
466
467 /// Adopt an existing nested-name-specifier (with source-range
468 /// information).
469 void Adopt(NestedNameSpecifierLoc Other);
470
471 /// Retrieve the source range covered by this nested-name-specifier.
472 SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) {
473 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
474 }
475
476 /// Retrieve a nested-name-specifier with location information,
477 /// copied into the given AST context.
478 ///
479 /// \param Context The context into which this nested-name-specifier will be
480 /// copied.
481 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
482
483 /// Retrieve a nested-name-specifier with location
484 /// information based on the information in this builder.
485 ///
486 /// This loc will contain references to the builder's internal data and may
487 /// be invalidated by any change to the builder.
488 NestedNameSpecifierLoc getTemporary() const {
489 return NestedNameSpecifierLoc(Representation, Buffer);
490 }
491
492 /// Clear out this builder, and prepare it to build another
493 /// nested-name-specifier with source-location information.
494 void Clear() {
495 Representation = nullptr;
496 BufferSize = 0;
497 }
498
499 /// Retrieve the underlying buffer.
500 ///
501 /// \returns A pair containing a pointer to the buffer of source-location
502 /// data and the size of the source-location data that resides in that
503 /// buffer.
504 std::pair<char *, unsigned> getBuffer() const {
505 return std::make_pair(Buffer, BufferSize);
506 }
507};
508
509/// Insertion operator for diagnostics. This allows sending
510/// NestedNameSpecifiers into a diagnostic with <<.
511inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
512 NestedNameSpecifier *NNS) {
513 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
514 DiagnosticsEngine::ak_nestednamespec);
515 return DB;
516}
517
518} // namespace clang
519
520#endif // LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H

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