/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/Scope.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Scope.cpp - Lexical scope information --------------------*- 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 the Scope class, which is used for recording |
10 | | // information about a lexical scope. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Sema/Scope.h" |
15 | | #include "clang/AST/Decl.h" |
16 | | #include "llvm/Support/raw_ostream.h" |
17 | | |
18 | | using namespace clang; |
19 | | |
20 | 37.1M | void Scope::setFlags(Scope *parent, unsigned flags) { |
21 | 37.1M | AnyParent = parent; |
22 | 37.1M | Flags = flags; |
23 | | |
24 | 37.1M | if (parent && !(flags & FnScope)37.1M ) { |
25 | 32.9M | BreakParent = parent->BreakParent; |
26 | 32.9M | ContinueParent = parent->ContinueParent; |
27 | 32.9M | } else { |
28 | | // Control scopes do not contain the contents of nested function scopes for |
29 | | // control flow purposes. |
30 | 4.24M | BreakParent = ContinueParent = nullptr; |
31 | 4.24M | } |
32 | | |
33 | 37.1M | if (parent) { |
34 | 37.1M | Depth = parent->Depth + 1; |
35 | 37.1M | PrototypeDepth = parent->PrototypeDepth; |
36 | 37.1M | PrototypeIndex = 0; |
37 | 37.1M | FnParent = parent->FnParent; |
38 | 37.1M | BlockParent = parent->BlockParent; |
39 | 37.1M | TemplateParamParent = parent->TemplateParamParent; |
40 | 37.1M | MSLastManglingParent = parent->MSLastManglingParent; |
41 | 37.1M | MSCurManglingNumber = getMSLastManglingNumber(); |
42 | 37.1M | if ((Flags & (FnScope | ClassScope | BlockScope | TemplateParamScope | |
43 | 37.1M | FunctionPrototypeScope | AtCatchScope | ObjCMethodScope)) == |
44 | 37.1M | 0) |
45 | 3.58M | Flags |= parent->getFlags() & OpenMPSimdDirectiveScope; |
46 | 37.1M | } else { |
47 | 88.2k | Depth = 0; |
48 | 88.2k | PrototypeDepth = 0; |
49 | 88.2k | PrototypeIndex = 0; |
50 | 88.2k | MSLastManglingParent = FnParent = BlockParent = nullptr; |
51 | 88.2k | TemplateParamParent = nullptr; |
52 | 88.2k | MSLastManglingNumber = 1; |
53 | 88.2k | MSCurManglingNumber = 1; |
54 | 88.2k | } |
55 | | |
56 | | // If this scope is a function or contains breaks/continues, remember it. |
57 | 37.1M | if (flags & FnScope) FnParent = this4.15M ; |
58 | | // The MS mangler uses the number of scopes that can hold declarations as |
59 | | // part of an external name. |
60 | 37.1M | if (Flags & (ClassScope | FnScope)) { |
61 | 5.68M | MSLastManglingNumber = getMSLastManglingNumber(); |
62 | 5.68M | MSLastManglingParent = this; |
63 | 5.68M | MSCurManglingNumber = 1; |
64 | 5.68M | } |
65 | 37.1M | if (flags & BreakScope) BreakParent = this59.9k ; |
66 | 37.1M | if (flags & ContinueScope) ContinueParent = this59.9k ; |
67 | 37.1M | if (flags & BlockScope) BlockParent = this11.9k ; |
68 | 37.1M | if (flags & TemplateParamScope) TemplateParamParent = this1.72M ; |
69 | | |
70 | | // If this is a prototype scope, record that. |
71 | 37.1M | if (flags & FunctionPrototypeScope) PrototypeDepth++26.1M ; |
72 | | |
73 | 37.1M | if (flags & DeclScope) { |
74 | 34.9M | if (flags & FunctionPrototypeScope) |
75 | 26.1M | ; // Prototype scopes are uninteresting. |
76 | 8.84M | else if ((flags & ClassScope) && getParent()->isClassScope()1.52M ) |
77 | 337k | ; // Nested class scopes aren't ambiguous. |
78 | 8.50M | else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope1.18M ) |
79 | 709k | ; // Classes inside of namespaces aren't ambiguous. |
80 | 7.79M | else if ((flags & EnumScope)) |
81 | 472k | ; // Don't increment for enum scopes. |
82 | 7.32M | else |
83 | 7.32M | incrementMSManglingNumber(); |
84 | 34.9M | } |
85 | 37.1M | } |
86 | | |
87 | 37.1M | void Scope::Init(Scope *parent, unsigned flags) { |
88 | 37.1M | setFlags(parent, flags); |
89 | | |
90 | 37.1M | DeclsInScope.clear(); |
91 | 37.1M | UsingDirectives.clear(); |
92 | 37.1M | Entity = nullptr; |
93 | 37.1M | ErrorTrap.reset(); |
94 | 37.1M | NRVO.setPointerAndInt(nullptr, false); |
95 | 37.1M | } |
96 | | |
97 | 33 | bool Scope::containedInPrototypeScope() const { |
98 | 33 | const Scope *S = this; |
99 | 81 | while (S) { |
100 | 51 | if (S->isFunctionPrototypeScope()) |
101 | 3 | return true; |
102 | 48 | S = S->getParent(); |
103 | 48 | } |
104 | 30 | return false; |
105 | 33 | } |
106 | | |
107 | 227k | void Scope::AddFlags(unsigned FlagsToSet) { |
108 | 227k | assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 && |
109 | 227k | "Unsupported scope flags"); |
110 | 227k | if (FlagsToSet & BreakScope) { |
111 | 227k | assert((Flags & BreakScope) == 0 && "Already set"); |
112 | 0 | BreakParent = this; |
113 | 227k | } |
114 | 227k | if (FlagsToSet & ContinueScope) { |
115 | 220k | assert((Flags & ContinueScope) == 0 && "Already set"); |
116 | 0 | ContinueParent = this; |
117 | 220k | } |
118 | 0 | Flags |= FlagsToSet; |
119 | 227k | } |
120 | | |
121 | 37.0M | void Scope::mergeNRVOIntoParent() { |
122 | 37.0M | if (VarDecl *Candidate = NRVO.getPointer()) { |
123 | 83.0k | if (isDeclScope(Candidate)) |
124 | 56.0k | Candidate->setNRVOVariable(true); |
125 | 83.0k | } |
126 | | |
127 | 37.0M | if (getEntity()) |
128 | 6.76M | return; |
129 | | |
130 | 30.2M | if (NRVO.getInt()) |
131 | 417k | getParent()->setNoNRVO(); |
132 | 29.8M | else if (NRVO.getPointer()) |
133 | 28.0k | getParent()->addNRVOCandidate(NRVO.getPointer()); |
134 | 30.2M | } |
135 | | |
136 | 0 | LLVM_DUMP_METHOD void Scope::dump() const { dumpImpl(llvm::errs()); } |
137 | | |
138 | 0 | void Scope::dumpImpl(raw_ostream &OS) const { |
139 | 0 | unsigned Flags = getFlags(); |
140 | 0 | bool HasFlags = Flags != 0; |
141 | |
|
142 | 0 | if (HasFlags) |
143 | 0 | OS << "Flags: "; |
144 | |
|
145 | 0 | std::pair<unsigned, const char *> FlagInfo[] = { |
146 | 0 | {FnScope, "FnScope"}, |
147 | 0 | {BreakScope, "BreakScope"}, |
148 | 0 | {ContinueScope, "ContinueScope"}, |
149 | 0 | {DeclScope, "DeclScope"}, |
150 | 0 | {ControlScope, "ControlScope"}, |
151 | 0 | {ClassScope, "ClassScope"}, |
152 | 0 | {BlockScope, "BlockScope"}, |
153 | 0 | {TemplateParamScope, "TemplateParamScope"}, |
154 | 0 | {FunctionPrototypeScope, "FunctionPrototypeScope"}, |
155 | 0 | {FunctionDeclarationScope, "FunctionDeclarationScope"}, |
156 | 0 | {AtCatchScope, "AtCatchScope"}, |
157 | 0 | {ObjCMethodScope, "ObjCMethodScope"}, |
158 | 0 | {SwitchScope, "SwitchScope"}, |
159 | 0 | {TryScope, "TryScope"}, |
160 | 0 | {FnTryCatchScope, "FnTryCatchScope"}, |
161 | 0 | {OpenMPDirectiveScope, "OpenMPDirectiveScope"}, |
162 | 0 | {OpenMPLoopDirectiveScope, "OpenMPLoopDirectiveScope"}, |
163 | 0 | {OpenMPSimdDirectiveScope, "OpenMPSimdDirectiveScope"}, |
164 | 0 | {EnumScope, "EnumScope"}, |
165 | 0 | {SEHTryScope, "SEHTryScope"}, |
166 | 0 | {SEHExceptScope, "SEHExceptScope"}, |
167 | 0 | {SEHFilterScope, "SEHFilterScope"}, |
168 | 0 | {CompoundStmtScope, "CompoundStmtScope"}, |
169 | 0 | {ClassInheritanceScope, "ClassInheritanceScope"}, |
170 | 0 | {CatchScope, "CatchScope"}, |
171 | 0 | }; |
172 | |
|
173 | 0 | for (auto Info : FlagInfo) { |
174 | 0 | if (Flags & Info.first) { |
175 | 0 | OS << Info.second; |
176 | 0 | Flags &= ~Info.first; |
177 | 0 | if (Flags) |
178 | 0 | OS << " | "; |
179 | 0 | } |
180 | 0 | } |
181 | |
|
182 | 0 | assert(Flags == 0 && "Unknown scope flags"); |
183 | | |
184 | 0 | if (HasFlags) |
185 | 0 | OS << '\n'; |
186 | |
|
187 | 0 | if (const Scope *Parent = getParent()) |
188 | 0 | OS << "Parent: (clang::Scope*)" << Parent << '\n'; |
189 | |
|
190 | 0 | OS << "Depth: " << Depth << '\n'; |
191 | 0 | OS << "MSLastManglingNumber: " << getMSLastManglingNumber() << '\n'; |
192 | 0 | OS << "MSCurManglingNumber: " << getMSCurManglingNumber() << '\n'; |
193 | 0 | if (const DeclContext *DC = getEntity()) |
194 | 0 | OS << "Entity : (clang::DeclContext*)" << DC << '\n'; |
195 | |
|
196 | 0 | if (NRVO.getInt()) |
197 | 0 | OS << "NRVO not allowed\n"; |
198 | 0 | else if (NRVO.getPointer()) |
199 | 0 | OS << "NRVO candidate : (clang::VarDecl*)" << NRVO.getPointer() << '\n'; |
200 | 0 | } |