/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Interpreter/InterpreterUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- InterpreterUtils.cpp - Incremental Utils --------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements some common utils used in the incremental library. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "InterpreterUtils.h" |
14 | | |
15 | | namespace clang { |
16 | | |
17 | 27 | IntegerLiteral *IntegerLiteralExpr(ASTContext &C, uint64_t Val) { |
18 | 27 | return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val), |
19 | 27 | C.UnsignedLongLongTy, SourceLocation()); |
20 | 27 | } |
21 | | |
22 | 27 | Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, Expr *E) { |
23 | 27 | ASTContext &Ctx = S.getASTContext(); |
24 | 27 | if (!Ty->isPointerType()) |
25 | 0 | Ty = Ctx.getPointerType(Ty); |
26 | | |
27 | 27 | TypeSourceInfo *TSI = Ctx.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
28 | 27 | Expr *Result = |
29 | 27 | S.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), E).get(); |
30 | 27 | assert(Result && "Cannot create CStyleCastPtrExpr"); |
31 | 27 | return Result; |
32 | 27 | } |
33 | | |
34 | 27 | Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, uintptr_t Ptr) { |
35 | 27 | ASTContext &Ctx = S.getASTContext(); |
36 | 27 | return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr)); |
37 | 27 | } |
38 | | |
39 | 0 | Sema::DeclGroupPtrTy CreateDGPtrFrom(Sema &S, Decl *D) { |
40 | 0 | SmallVector<Decl *, 1> DeclsInGroup; |
41 | 0 | DeclsInGroup.push_back(D); |
42 | 0 | Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup); |
43 | 0 | return DeclGroupPtr; |
44 | 0 | } |
45 | | |
46 | | NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name, |
47 | 0 | const DeclContext *Within) { |
48 | 0 | DeclarationName DName = &S.Context.Idents.get(Name); |
49 | 0 | LookupResult R(S, DName, SourceLocation(), |
50 | 0 | Sema::LookupNestedNameSpecifierName); |
51 | 0 | R.suppressDiagnostics(); |
52 | 0 | if (!Within) |
53 | 0 | S.LookupName(R, S.TUScope); |
54 | 0 | else { |
55 | 0 | if (const auto *TD = dyn_cast<clang::TagDecl>(Within); |
56 | 0 | TD && !TD->getDefinition()) |
57 | | // No definition, no lookup result. |
58 | 0 | return nullptr; |
59 | | |
60 | 0 | S.LookupQualifiedName(R, const_cast<DeclContext *>(Within)); |
61 | 0 | } |
62 | | |
63 | 0 | if (R.empty()) |
64 | 0 | return nullptr; |
65 | | |
66 | 0 | R.resolveKind(); |
67 | |
|
68 | 0 | return dyn_cast<NamespaceDecl>(R.getFoundDecl()); |
69 | 0 | } |
70 | | |
71 | | NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name, |
72 | 0 | const DeclContext *Within) { |
73 | 0 | DeclarationName DName = &S.Context.Idents.get(Name); |
74 | 0 | LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName, |
75 | 0 | Sema::ForVisibleRedeclaration); |
76 | |
|
77 | 0 | R.suppressDiagnostics(); |
78 | |
|
79 | 0 | if (!Within) |
80 | 0 | S.LookupName(R, S.TUScope); |
81 | 0 | else { |
82 | 0 | const DeclContext *PrimaryWithin = nullptr; |
83 | 0 | if (const auto *TD = dyn_cast<TagDecl>(Within)) |
84 | 0 | PrimaryWithin = llvm::dyn_cast_or_null<DeclContext>(TD->getDefinition()); |
85 | 0 | else |
86 | 0 | PrimaryWithin = Within->getPrimaryContext(); |
87 | | |
88 | | // No definition, no lookup result. |
89 | 0 | if (!PrimaryWithin) |
90 | 0 | return nullptr; |
91 | | |
92 | 0 | S.LookupQualifiedName(R, const_cast<DeclContext *>(PrimaryWithin)); |
93 | 0 | } |
94 | | |
95 | 0 | if (R.empty()) |
96 | 0 | return nullptr; |
97 | 0 | R.resolveKind(); |
98 | |
|
99 | 0 | if (R.isSingleResult()) |
100 | 0 | return llvm::dyn_cast<NamedDecl>(R.getFoundDecl()); |
101 | | |
102 | 0 | return nullptr; |
103 | 0 | } |
104 | | |
105 | 0 | std::string GetFullTypeName(ASTContext &Ctx, QualType QT) { |
106 | 0 | PrintingPolicy Policy(Ctx.getPrintingPolicy()); |
107 | 0 | Policy.SuppressScope = false; |
108 | 0 | Policy.AnonymousTagLocations = false; |
109 | 0 | return QT.getAsString(Policy); |
110 | 0 | } |
111 | | } // namespace clang |