clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name QualTypeNames.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 -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include/clang/AST -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../include -I /usr/src/gnu/usr.bin/clang/libclangAST/obj -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/QualTypeNames.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "clang/AST/DeclTemplate.h" |
10 | #include "clang/AST/DeclarationName.h" |
11 | #include "clang/AST/GlobalDecl.h" |
12 | #include "clang/AST/Mangle.h" |
13 | #include "clang/AST/QualTypeNames.h" |
14 | |
15 | #include <stdio.h> |
16 | #include <memory> |
17 | |
18 | namespace clang { |
19 | |
20 | namespace TypeName { |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | static NestedNameSpecifier *createNestedNameSpecifier( |
31 | const ASTContext &Ctx, |
32 | const NamespaceDecl *Namesp, |
33 | bool WithGlobalNsPrefix); |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | static NestedNameSpecifier *createNestedNameSpecifier( |
46 | const ASTContext &Ctx, const TypeDecl *TD, |
47 | bool FullyQualify, bool WithGlobalNsPrefix); |
48 | |
49 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
50 | const ASTContext &Ctx, const Decl *decl, |
51 | bool FullyQualified, bool WithGlobalNsPrefix); |
52 | |
53 | static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier( |
54 | const ASTContext &Ctx, NestedNameSpecifier *scope, bool WithGlobalNsPrefix); |
55 | |
56 | static bool getFullyQualifiedTemplateName(const ASTContext &Ctx, |
57 | TemplateName &TName, |
58 | bool WithGlobalNsPrefix) { |
59 | bool Changed = false; |
60 | NestedNameSpecifier *NNS = nullptr; |
61 | |
62 | TemplateDecl *ArgTDecl = TName.getAsTemplateDecl(); |
63 | |
64 | |
65 | assert(ArgTDecl != nullptr); |
66 | QualifiedTemplateName *QTName = TName.getAsQualifiedTemplateName(); |
67 | |
68 | if (QTName && !QTName->hasTemplateKeyword()) { |
| 1 | Assuming 'QTName' is null | |
|
69 | NNS = QTName->getQualifier(); |
70 | NestedNameSpecifier *QNNS = getFullyQualifiedNestedNameSpecifier( |
71 | Ctx, NNS, WithGlobalNsPrefix); |
72 | if (QNNS != NNS) { |
73 | Changed = true; |
74 | NNS = QNNS; |
75 | } else { |
76 | NNS = nullptr; |
77 | } |
78 | } else { |
79 | NNS = createNestedNameSpecifierForScopeOf( |
| 2 | | Calling 'createNestedNameSpecifierForScopeOf' | |
|
80 | Ctx, ArgTDecl, true, WithGlobalNsPrefix); |
81 | } |
82 | if (NNS) { |
83 | TName = Ctx.getQualifiedTemplateName(NNS, |
84 | false, ArgTDecl); |
85 | Changed = true; |
86 | } |
87 | return Changed; |
88 | } |
89 | |
90 | static bool getFullyQualifiedTemplateArgument(const ASTContext &Ctx, |
91 | TemplateArgument &Arg, |
92 | bool WithGlobalNsPrefix) { |
93 | bool Changed = false; |
94 | |
95 | |
96 | |
97 | |
98 | if (Arg.getKind() == TemplateArgument::Template) { |
99 | TemplateName TName = Arg.getAsTemplate(); |
100 | Changed = getFullyQualifiedTemplateName(Ctx, TName, WithGlobalNsPrefix); |
101 | if (Changed) { |
102 | Arg = TemplateArgument(TName); |
103 | } |
104 | } else if (Arg.getKind() == TemplateArgument::Type) { |
105 | QualType SubTy = Arg.getAsType(); |
106 | |
107 | QualType QTFQ = getFullyQualifiedType(SubTy, Ctx, WithGlobalNsPrefix); |
108 | if (QTFQ != SubTy) { |
109 | Arg = TemplateArgument(QTFQ); |
110 | Changed = true; |
111 | } |
112 | } |
113 | return Changed; |
114 | } |
115 | |
116 | static const Type *getFullyQualifiedTemplateType(const ASTContext &Ctx, |
117 | const Type *TypePtr, |
118 | bool WithGlobalNsPrefix) { |
119 | |
120 | |
121 | |
122 | assert(!isa<DependentTemplateSpecializationType>(TypePtr)); |
123 | |
124 | |
125 | if (const auto *TST = dyn_cast<const TemplateSpecializationType>(TypePtr)) { |
126 | bool MightHaveChanged = false; |
127 | SmallVector<TemplateArgument, 4> FQArgs; |
128 | for (TemplateSpecializationType::iterator I = TST->begin(), E = TST->end(); |
129 | I != E; ++I) { |
130 | |
131 | |
132 | TemplateArgument Arg(*I); |
133 | MightHaveChanged |= getFullyQualifiedTemplateArgument( |
134 | Ctx, Arg, WithGlobalNsPrefix); |
135 | FQArgs.push_back(Arg); |
136 | } |
137 | |
138 | |
139 | |
140 | if (MightHaveChanged) { |
141 | QualType QT = Ctx.getTemplateSpecializationType( |
142 | TST->getTemplateName(), FQArgs, |
143 | TST->getCanonicalTypeInternal()); |
144 | |
145 | |
146 | |
147 | return QT.getTypePtr(); |
148 | } |
149 | } else if (const auto *TSTRecord = dyn_cast<const RecordType>(TypePtr)) { |
150 | |
151 | |
152 | |
153 | |
154 | if (const auto *TSTDecl = |
155 | dyn_cast<ClassTemplateSpecializationDecl>(TSTRecord->getDecl())) { |
156 | const TemplateArgumentList &TemplateArgs = TSTDecl->getTemplateArgs(); |
157 | |
158 | bool MightHaveChanged = false; |
159 | SmallVector<TemplateArgument, 4> FQArgs; |
160 | for (unsigned int I = 0, E = TemplateArgs.size(); I != E; ++I) { |
161 | |
162 | |
163 | TemplateArgument Arg(TemplateArgs[I]); |
164 | MightHaveChanged |= getFullyQualifiedTemplateArgument( |
165 | Ctx, Arg, WithGlobalNsPrefix); |
166 | FQArgs.push_back(Arg); |
167 | } |
168 | |
169 | |
170 | |
171 | if (MightHaveChanged) { |
172 | TemplateName TN(TSTDecl->getSpecializedTemplate()); |
173 | QualType QT = Ctx.getTemplateSpecializationType( |
174 | TN, FQArgs, |
175 | TSTRecord->getCanonicalTypeInternal()); |
176 | |
177 | |
178 | |
179 | return QT.getTypePtr(); |
180 | } |
181 | } |
182 | } |
183 | return TypePtr; |
184 | } |
185 | |
186 | static NestedNameSpecifier *createOuterNNS(const ASTContext &Ctx, const Decl *D, |
187 | bool FullyQualify, |
188 | bool WithGlobalNsPrefix) { |
189 | const DeclContext *DC = D->getDeclContext(); |
190 | if (const auto *NS = dyn_cast<NamespaceDecl>(DC)) { |
191 | while (NS && NS->isInline()) { |
192 | |
193 | NS = dyn_cast<NamespaceDecl>(NS->getDeclContext()); |
194 | } |
195 | if (NS && NS->getDeclName()) { |
196 | return createNestedNameSpecifier(Ctx, NS, WithGlobalNsPrefix); |
197 | } |
198 | return nullptr; |
199 | } else if (const auto *TD = dyn_cast<TagDecl>(DC)) { |
200 | return createNestedNameSpecifier(Ctx, TD, FullyQualify, WithGlobalNsPrefix); |
201 | } else if (const auto *TDD = dyn_cast<TypedefNameDecl>(DC)) { |
202 | return createNestedNameSpecifier( |
203 | Ctx, TDD, FullyQualify, WithGlobalNsPrefix); |
204 | } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) { |
205 | return NestedNameSpecifier::GlobalSpecifier(Ctx); |
206 | } |
207 | return nullptr; |
208 | } |
209 | |
210 | |
211 | static NestedNameSpecifier *getFullyQualifiedNestedNameSpecifier( |
212 | const ASTContext &Ctx, NestedNameSpecifier *Scope, |
213 | bool WithGlobalNsPrefix) { |
214 | switch (Scope->getKind()) { |
215 | case NestedNameSpecifier::Global: |
216 | |
217 | return Scope; |
218 | case NestedNameSpecifier::Namespace: |
219 | return TypeName::createNestedNameSpecifier( |
220 | Ctx, Scope->getAsNamespace(), WithGlobalNsPrefix); |
221 | case NestedNameSpecifier::NamespaceAlias: |
222 | |
223 | |
224 | |
225 | |
226 | return TypeName::createNestedNameSpecifier( |
227 | Ctx, |
228 | Scope->getAsNamespaceAlias()->getNamespace()->getCanonicalDecl(), |
229 | WithGlobalNsPrefix); |
230 | case NestedNameSpecifier::Identifier: |
231 | |
232 | |
233 | |
234 | return getFullyQualifiedNestedNameSpecifier( |
235 | Ctx, Scope->getPrefix(), WithGlobalNsPrefix); |
236 | case NestedNameSpecifier::Super: |
237 | case NestedNameSpecifier::TypeSpec: |
238 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
239 | const Type *Type = Scope->getAsType(); |
240 | |
241 | const TagDecl *TD = nullptr; |
242 | if (const TagType *TagDeclType = Type->getAs<TagType>()) { |
243 | TD = TagDeclType->getDecl(); |
244 | } else { |
245 | TD = Type->getAsCXXRecordDecl(); |
246 | } |
247 | if (TD) { |
248 | return TypeName::createNestedNameSpecifier(Ctx, TD, |
249 | true , |
250 | WithGlobalNsPrefix); |
251 | } else if (const auto *TDD = dyn_cast<TypedefType>(Type)) { |
252 | return TypeName::createNestedNameSpecifier(Ctx, TDD->getDecl(), |
253 | true , |
254 | WithGlobalNsPrefix); |
255 | } |
256 | return Scope; |
257 | } |
258 | } |
259 | llvm_unreachable("bad NNS kind"); |
260 | } |
261 | |
262 | |
263 | |
264 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
265 | const ASTContext &Ctx, const Decl *Decl, |
266 | bool FullyQualified, bool WithGlobalNsPrefix) { |
267 | assert(Decl); |
268 | |
269 | const DeclContext *DC = Decl->getDeclContext()->getRedeclContext(); |
| |
270 | const auto *Outer = dyn_cast_or_null<NamedDecl>(DC); |
| 4 | | Assuming null pointer is passed into cast | |
|
| 5 | | Assuming pointer value is null | |
|
271 | const auto *OuterNS = dyn_cast_or_null<NamespaceDecl>(DC); |
| 6 | | Assuming null pointer is passed into cast | |
|
272 | if (Outer && !(OuterNS && OuterNS->isAnonymousNamespace())) { |
273 | if (const auto *CxxDecl = dyn_cast<CXXRecordDecl>(DC)) { |
274 | if (ClassTemplateDecl *ClassTempl = |
275 | CxxDecl->getDescribedClassTemplate()) { |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | |
286 | if (ClassTempl->spec_begin() != ClassTempl->spec_end()) { |
287 | Decl = *(ClassTempl->spec_begin()); |
288 | Outer = dyn_cast<NamedDecl>(Decl); |
289 | OuterNS = dyn_cast<NamespaceDecl>(Decl); |
290 | } |
291 | } |
292 | } |
293 | |
294 | if (OuterNS) { |
295 | return createNestedNameSpecifier(Ctx, OuterNS, WithGlobalNsPrefix); |
296 | } else if (const auto *TD = dyn_cast<TagDecl>(Outer)) { |
297 | return createNestedNameSpecifier( |
298 | Ctx, TD, FullyQualified, WithGlobalNsPrefix); |
299 | } else if (dyn_cast<TranslationUnitDecl>(Outer)) { |
300 | |
301 | return nullptr; |
302 | } else { |
303 | |
304 | |
305 | |
306 | return nullptr; |
307 | } |
308 | } else if (WithGlobalNsPrefix && DC->isTranslationUnit()) { |
| 7 | | Assuming 'WithGlobalNsPrefix' is true | |
|
| 8 | | Called C++ object pointer is null |
|
309 | return NestedNameSpecifier::GlobalSpecifier(Ctx); |
310 | } |
311 | return nullptr; |
312 | } |
313 | |
314 | |
315 | |
316 | static NestedNameSpecifier *createNestedNameSpecifierForScopeOf( |
317 | const ASTContext &Ctx, const Type *TypePtr, |
318 | bool FullyQualified, bool WithGlobalNsPrefix) { |
319 | if (!TypePtr) return nullptr; |
320 | |
321 | Decl *Decl = nullptr; |
322 | |
323 | if (const auto *TDT = dyn_cast<TypedefType>(TypePtr)) { |
324 | Decl = TDT->getDecl(); |
325 | } else if (const auto *TagDeclType = dyn_cast<TagType>(TypePtr)) { |
326 | Decl = TagDeclType->getDecl(); |
327 | } else if (const auto *TST = dyn_cast<TemplateSpecializationType>(TypePtr)) { |
328 | Decl = TST->getTemplateName().getAsTemplateDecl(); |
329 | } else { |
330 | Decl = TypePtr->getAsCXXRecordDecl(); |
331 | } |
332 | |
333 | if (!Decl) return nullptr; |
334 | |
335 | return createNestedNameSpecifierForScopeOf( |
336 | Ctx, Decl, FullyQualified, WithGlobalNsPrefix); |
337 | } |
338 | |
339 | NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx, |
340 | const NamespaceDecl *Namespace, |
341 | bool WithGlobalNsPrefix) { |
342 | while (Namespace && Namespace->isInline()) { |
343 | |
344 | Namespace = dyn_cast<NamespaceDecl>(Namespace->getDeclContext()); |
345 | } |
346 | if (!Namespace) return nullptr; |
347 | |
348 | bool FullyQualified = true; |
349 | return NestedNameSpecifier::Create( |
350 | Ctx, |
351 | createOuterNNS(Ctx, Namespace, FullyQualified, WithGlobalNsPrefix), |
352 | Namespace); |
353 | } |
354 | |
355 | NestedNameSpecifier *createNestedNameSpecifier(const ASTContext &Ctx, |
356 | const TypeDecl *TD, |
357 | bool FullyQualify, |
358 | bool WithGlobalNsPrefix) { |
359 | const Type *TypePtr = TD->getTypeForDecl(); |
360 | if (isa<const TemplateSpecializationType>(TypePtr) || |
361 | isa<const RecordType>(TypePtr)) { |
362 | |
363 | |
364 | |
365 | |
366 | TypePtr = getFullyQualifiedTemplateType(Ctx, TypePtr, WithGlobalNsPrefix); |
367 | } |
368 | |
369 | return NestedNameSpecifier::Create( |
370 | Ctx, createOuterNNS(Ctx, TD, FullyQualify, WithGlobalNsPrefix), |
371 | false , TypePtr); |
372 | } |
373 | |
374 | |
375 | |
376 | QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx, |
377 | bool WithGlobalNsPrefix) { |
378 | |
379 | |
380 | if (isa<PointerType>(QT.getTypePtr())) { |
381 | |
382 | Qualifiers Quals = QT.getQualifiers(); |
383 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
384 | QT = Ctx.getPointerType(QT); |
385 | |
386 | QT = Ctx.getQualifiedType(QT, Quals); |
387 | return QT; |
388 | } |
389 | |
390 | if (auto *MPT = dyn_cast<MemberPointerType>(QT.getTypePtr())) { |
391 | |
392 | Qualifiers Quals = QT.getQualifiers(); |
393 | |
394 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
395 | QualType Class = getFullyQualifiedType(QualType(MPT->getClass(), 0), Ctx, |
396 | WithGlobalNsPrefix); |
397 | QT = Ctx.getMemberPointerType(QT, Class.getTypePtr()); |
398 | |
399 | QT = Ctx.getQualifiedType(QT, Quals); |
400 | return QT; |
401 | } |
402 | |
403 | |
404 | |
405 | if (isa<ReferenceType>(QT.getTypePtr())) { |
406 | |
407 | bool IsLValueRefTy = isa<LValueReferenceType>(QT.getTypePtr()); |
408 | Qualifiers Quals = QT.getQualifiers(); |
409 | QT = getFullyQualifiedType(QT->getPointeeType(), Ctx, WithGlobalNsPrefix); |
410 | |
411 | |
412 | if (IsLValueRefTy) |
413 | QT = Ctx.getLValueReferenceType(QT); |
414 | else |
415 | QT = Ctx.getRValueReferenceType(QT); |
416 | |
417 | QT = Ctx.getQualifiedType(QT, Quals); |
418 | return QT; |
419 | } |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | while (isa<SubstTemplateTypeParmType>(QT.getTypePtr())) { |
426 | |
427 | Qualifiers Quals = QT.getQualifiers(); |
428 | |
429 | QT = cast<SubstTemplateTypeParmType>(QT.getTypePtr())->desugar(); |
430 | |
431 | |
432 | QT = Ctx.getQualifiedType(QT, Quals); |
433 | } |
434 | |
435 | NestedNameSpecifier *Prefix = nullptr; |
436 | |
437 | |
438 | |
439 | Qualifiers PrefixQualifiers = QT.getLocalQualifiers(); |
440 | QT = QualType(QT.getTypePtr(), 0); |
441 | ElaboratedTypeKeyword Keyword = ETK_None; |
442 | if (const auto *ETypeInput = dyn_cast<ElaboratedType>(QT.getTypePtr())) { |
443 | QT = ETypeInput->getNamedType(); |
444 | assert(!QT.hasLocalQualifiers()); |
445 | Keyword = ETypeInput->getKeyword(); |
446 | } |
447 | |
448 | Prefix = createNestedNameSpecifierForScopeOf(Ctx, QT.getTypePtr(), |
449 | true , |
450 | WithGlobalNsPrefix); |
451 | |
452 | |
453 | |
454 | if (isa<const TemplateSpecializationType>(QT.getTypePtr()) || |
455 | isa<const RecordType>(QT.getTypePtr())) { |
456 | |
457 | |
458 | |
459 | |
460 | const Type *TypePtr = getFullyQualifiedTemplateType( |
461 | Ctx, QT.getTypePtr(), WithGlobalNsPrefix); |
462 | QT = QualType(TypePtr, 0); |
463 | } |
464 | if (Prefix || Keyword != ETK_None) { |
465 | QT = Ctx.getElaboratedType(Keyword, Prefix, QT); |
466 | } |
467 | QT = Ctx.getQualifiedType(QT, PrefixQualifiers); |
468 | return QT; |
469 | } |
470 | |
471 | std::string getFullyQualifiedName(QualType QT, |
472 | const ASTContext &Ctx, |
473 | const PrintingPolicy &Policy, |
474 | bool WithGlobalNsPrefix) { |
475 | QualType FQQT = getFullyQualifiedType(QT, Ctx, WithGlobalNsPrefix); |
476 | return FQQT.getAsString(Policy); |
477 | } |
478 | |
479 | } |
480 | } |