1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_SEMA_SEMAINTERNAL_H |
15 | #define LLVM_CLANG_SEMA_SEMAINTERNAL_H |
16 | |
17 | #include "clang/AST/ASTContext.h" |
18 | #include "clang/Sema/Lookup.h" |
19 | #include "clang/Sema/Sema.h" |
20 | #include "clang/Sema/SemaDiagnostic.h" |
21 | |
22 | namespace clang { |
23 | |
24 | inline PartialDiagnostic Sema::PDiag(unsigned DiagID) { |
25 | return PartialDiagnostic(DiagID, Context.getDiagAllocator()); |
26 | } |
27 | |
28 | inline bool |
29 | FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo &FTI) { |
30 | return FTI.NumParams == 1 && !FTI.isVariadic && |
31 | FTI.Params[0].Ident == nullptr && FTI.Params[0].Param && |
32 | cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType(); |
33 | } |
34 | |
35 | inline bool |
36 | FTIHasNonVoidParameters(const DeclaratorChunk::FunctionTypeInfo &FTI) { |
37 | |
38 | return FTI.NumParams && !FTIHasSingleVoidParameter(FTI); |
39 | } |
40 | |
41 | |
42 | |
43 | inline bool IsVariableAConstantExpression(VarDecl *Var, ASTContext &Context) { |
44 | const VarDecl *DefVD = nullptr; |
45 | return !isa<ParmVarDecl>(Var) && |
46 | Var->isUsableInConstantExpressions(Context) && |
47 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE(); |
48 | } |
49 | |
50 | |
51 | |
52 | |
53 | inline bool DeclAttrsMatchCUDAMode(const LangOptions &LangOpts, Decl *D) { |
54 | if (!LangOpts.CUDA || !D) |
55 | return true; |
56 | bool isDeviceSideDecl = D->hasAttr<CUDADeviceAttr>() || |
57 | D->hasAttr<CUDASharedAttr>() || |
58 | D->hasAttr<CUDAGlobalAttr>(); |
59 | return isDeviceSideDecl == LangOpts.CUDAIsDevice; |
60 | } |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | inline void MarkVarDeclODRUsed(VarDecl *Var, |
70 | SourceLocation Loc, Sema &SemaRef, |
71 | const unsigned *const FunctionScopeIndexToStopAt) { |
72 | |
73 | |
74 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && |
75 | (!Var->isExternallyVisible() || Var->isInline() || |
76 | SemaRef.isExternalWithNoLinkageType(Var)) && |
77 | !(Var->isStaticDataMember() && Var->hasInit())) { |
78 | SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; |
79 | if (old.isInvalid()) |
80 | old = Loc; |
81 | } |
82 | QualType CaptureType, DeclRefType; |
83 | SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, |
84 | SourceLocation(), |
85 | true, |
86 | CaptureType, DeclRefType, |
87 | FunctionScopeIndexToStopAt); |
88 | |
89 | Var->markUsed(SemaRef.Context); |
90 | } |
91 | |
92 | |
93 | inline InheritableAttr *getDLLAttr(Decl *D) { |
94 | (0) . __assert_fail ("!(D->hasAttr() && D->hasAttr()) && \"A declaration cannot be both dllimport and dllexport.\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/SemaInternal.h", 95, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!(D->hasAttr<DLLImportAttr>() && D->hasAttr<DLLExportAttr>()) && |
95 | (0) . __assert_fail ("!(D->hasAttr() && D->hasAttr()) && \"A declaration cannot be both dllimport and dllexport.\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Sema/SemaInternal.h", 95, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "A declaration cannot be both dllimport and dllexport."); |
96 | if (auto *Import = D->getAttr<DLLImportAttr>()) |
97 | return Import; |
98 | if (auto *Export = D->getAttr<DLLExportAttr>()) |
99 | return Export; |
100 | return nullptr; |
101 | } |
102 | |
103 | |
104 | inline std::pair<unsigned, unsigned> getDepthAndIndex(NamedDecl *ND) { |
105 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
106 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
107 | |
108 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
109 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
110 | |
111 | const auto *TTP = cast<TemplateTemplateParmDecl>(ND); |
112 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
113 | } |
114 | |
115 | |
116 | inline std::pair<unsigned, unsigned> |
117 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
118 | if (const auto *TTP = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
119 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
120 | |
121 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
122 | } |
123 | |
124 | class TypoCorrectionConsumer : public VisibleDeclConsumer { |
125 | typedef SmallVector<TypoCorrection, 1> TypoResultList; |
126 | typedef llvm::StringMap<TypoResultList> TypoResultsMap; |
127 | typedef std::map<unsigned, TypoResultsMap> TypoEditDistanceMap; |
128 | |
129 | public: |
130 | TypoCorrectionConsumer(Sema &SemaRef, |
131 | const DeclarationNameInfo &TypoName, |
132 | Sema::LookupNameKind LookupKind, |
133 | Scope *S, CXXScopeSpec *SS, |
134 | std::unique_ptr<CorrectionCandidateCallback> CCC, |
135 | DeclContext *MemberContext, |
136 | bool EnteringContext) |
137 | : Typo(TypoName.getName().getAsIdentifierInfo()), CurrentTCIndex(0), |
138 | SavedTCIndex(0), SemaRef(SemaRef), S(S), |
139 | SS(SS ? llvm::make_unique<CXXScopeSpec>(*SS) : nullptr), |
140 | CorrectionValidator(std::move(CCC)), MemberContext(MemberContext), |
141 | Result(SemaRef, TypoName, LookupKind), |
142 | Namespaces(SemaRef.Context, SemaRef.CurContext, SS), |
143 | EnteringContext(EnteringContext), SearchNamespaces(false) { |
144 | Result.suppressDiagnostics(); |
145 | |
146 | ValidatedCorrections.push_back(TypoCorrection()); |
147 | } |
148 | |
149 | bool includeHiddenDecls() const override { return true; } |
150 | |
151 | |
152 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
153 | bool InBaseClass) override; |
154 | void FoundName(StringRef Name); |
155 | void addKeywordResult(StringRef Keyword); |
156 | void addCorrection(TypoCorrection Correction); |
157 | |
158 | bool empty() const { |
159 | return CorrectionResults.empty() && ValidatedCorrections.size() == 1; |
160 | } |
161 | |
162 | |
163 | |
164 | TypoResultList &operator[](StringRef Name) { |
165 | return CorrectionResults.begin()->second[Name]; |
166 | } |
167 | |
168 | |
169 | |
170 | unsigned getBestEditDistance(bool Normalized) { |
171 | if (CorrectionResults.empty()) |
172 | return (std::numeric_limits<unsigned>::max)(); |
173 | |
174 | unsigned BestED = CorrectionResults.begin()->first; |
175 | return Normalized ? TypoCorrection::NormalizeEditDistance(BestED) : BestED; |
176 | } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | void |
183 | addNamespaces(const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces); |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | const TypoCorrection &getNextCorrection(); |
191 | |
192 | |
193 | const TypoCorrection &getCurrentCorrection() { |
194 | return CurrentTCIndex < ValidatedCorrections.size() |
195 | ? ValidatedCorrections[CurrentTCIndex] |
196 | : ValidatedCorrections[0]; |
197 | } |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | const TypoCorrection &peekNextCorrection() { |
204 | auto Current = CurrentTCIndex; |
205 | const TypoCorrection &TC = getNextCorrection(); |
206 | CurrentTCIndex = Current; |
207 | return TC; |
208 | } |
209 | |
210 | |
211 | |
212 | |
213 | void resetCorrectionStream() { |
214 | CurrentTCIndex = 0; |
215 | } |
216 | |
217 | |
218 | |
219 | bool finished() { |
220 | return CorrectionResults.empty() && |
221 | CurrentTCIndex >= ValidatedCorrections.size(); |
222 | } |
223 | |
224 | |
225 | |
226 | void saveCurrentPosition() { |
227 | SavedTCIndex = CurrentTCIndex; |
228 | } |
229 | |
230 | |
231 | void restoreSavedPosition() { |
232 | CurrentTCIndex = SavedTCIndex; |
233 | } |
234 | |
235 | ASTContext &getContext() const { return SemaRef.Context; } |
236 | const LookupResult &getLookupResult() const { return Result; } |
237 | |
238 | bool isAddressOfOperand() const { return CorrectionValidator->IsAddressOfOperand; } |
239 | const CXXScopeSpec *getSS() const { return SS.get(); } |
240 | Scope *getScope() const { return S; } |
241 | CorrectionCandidateCallback *getCorrectionValidator() const { |
242 | return CorrectionValidator.get(); |
243 | } |
244 | |
245 | private: |
246 | class NamespaceSpecifierSet { |
247 | struct SpecifierInfo { |
248 | DeclContext* DeclCtx; |
249 | NestedNameSpecifier* NameSpecifier; |
250 | unsigned EditDistance; |
251 | }; |
252 | |
253 | typedef SmallVector<DeclContext*, 4> DeclContextList; |
254 | typedef SmallVector<SpecifierInfo, 16> SpecifierInfoList; |
255 | |
256 | ASTContext &Context; |
257 | DeclContextList CurContextChain; |
258 | std::string CurNameSpecifier; |
259 | SmallVector<const IdentifierInfo*, 4> CurContextIdentifiers; |
260 | SmallVector<const IdentifierInfo*, 4> CurNameSpecifierIdentifiers; |
261 | |
262 | std::map<unsigned, SpecifierInfoList> DistanceMap; |
263 | |
264 | |
265 | |
266 | static DeclContextList buildContextChain(DeclContext *Start); |
267 | |
268 | unsigned buildNestedNameSpecifier(DeclContextList &DeclChain, |
269 | NestedNameSpecifier *&NNS); |
270 | |
271 | public: |
272 | NamespaceSpecifierSet(ASTContext &Context, DeclContext *CurContext, |
273 | CXXScopeSpec *CurScopeSpec); |
274 | |
275 | |
276 | |
277 | void addNameSpecifier(DeclContext *Ctx); |
278 | |
279 | |
280 | class iterator |
281 | : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag, |
282 | SpecifierInfo> { |
283 | |
284 | const std::map<unsigned, SpecifierInfoList>::iterator OuterBack; |
285 | |
286 | std::map<unsigned, SpecifierInfoList>::iterator Outer; |
287 | |
288 | SpecifierInfoList::iterator Inner; |
289 | |
290 | public: |
291 | iterator(NamespaceSpecifierSet &Set, bool IsAtEnd) |
292 | : OuterBack(std::prev(Set.DistanceMap.end())), |
293 | Outer(Set.DistanceMap.begin()), |
294 | Inner(!IsAtEnd ? Outer->second.begin() : OuterBack->second.end()) { |
295 | assert(!Set.DistanceMap.empty()); |
296 | } |
297 | |
298 | iterator &operator++() { |
299 | ++Inner; |
300 | if (Inner == Outer->second.end() && Outer != OuterBack) { |
301 | ++Outer; |
302 | Inner = Outer->second.begin(); |
303 | } |
304 | return *this; |
305 | } |
306 | |
307 | SpecifierInfo &operator*() { return *Inner; } |
308 | bool operator==(const iterator &RHS) const { return Inner == RHS.Inner; } |
309 | }; |
310 | |
311 | iterator begin() { return iterator(*this, ); } |
312 | iterator end() { return iterator(*this, ); } |
313 | }; |
314 | |
315 | void addName(StringRef Name, NamedDecl *ND, |
316 | NestedNameSpecifier *NNS = nullptr, bool isKeyword = false); |
317 | |
318 | |
319 | |
320 | |
321 | bool resolveCorrection(TypoCorrection &Candidate); |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | void performQualifiedLookups(); |
328 | |
329 | |
330 | IdentifierInfo *Typo; |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | TypoEditDistanceMap CorrectionResults; |
338 | |
339 | SmallVector<TypoCorrection, 4> ValidatedCorrections; |
340 | size_t CurrentTCIndex; |
341 | size_t SavedTCIndex; |
342 | |
343 | Sema &SemaRef; |
344 | Scope *S; |
345 | std::unique_ptr<CXXScopeSpec> SS; |
346 | std::unique_ptr<CorrectionCandidateCallback> CorrectionValidator; |
347 | DeclContext *MemberContext; |
348 | LookupResult Result; |
349 | NamespaceSpecifierSet Namespaces; |
350 | SmallVector<TypoCorrection, 2> QualifiedResults; |
351 | bool EnteringContext; |
352 | bool SearchNamespaces; |
353 | }; |
354 | |
355 | inline Sema::TypoExprState::TypoExprState() {} |
356 | |
357 | inline Sema::TypoExprState::TypoExprState(TypoExprState &&other) noexcept { |
358 | *this = std::move(other); |
359 | } |
360 | |
361 | inline Sema::TypoExprState &Sema::TypoExprState:: |
362 | operator=(Sema::TypoExprState &&other) noexcept { |
363 | Consumer = std::move(other.Consumer); |
364 | DiagHandler = std::move(other.DiagHandler); |
365 | RecoveryHandler = std::move(other.RecoveryHandler); |
366 | return *this; |
367 | } |
368 | |
369 | } |
370 | |
371 | #endif |
372 | |