Mercurial > hg > CbC > CbC_llvm
comparison clang-tools-extra/clangd/AST.cpp @ 221:79ff65ed7e25
LLVM12 Original
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 15 Jun 2021 19:15:29 +0900 |
parents | 0572611fdcc8 |
children | c4bab56944e8 |
comparison
equal
deleted
inserted
replaced
220:42394fc6a535 | 221:79ff65ed7e25 |
---|---|
114 | 114 |
115 NestedNameSpecifier *NNS = nullptr; | 115 NestedNameSpecifier *NNS = nullptr; |
116 if (auto *TD = llvm::dyn_cast<TagDecl>(CurContext)) { | 116 if (auto *TD = llvm::dyn_cast<TagDecl>(CurContext)) { |
117 // There can't be any more tag parents after hitting a namespace. | 117 // There can't be any more tag parents after hitting a namespace. |
118 assert(!ReachedNS); | 118 assert(!ReachedNS); |
119 (void)ReachedNS; | |
119 NNS = NestedNameSpecifier::Create(Context, nullptr, false, | 120 NNS = NestedNameSpecifier::Create(Context, nullptr, false, |
120 TD->getTypeForDecl()); | 121 TD->getTypeForDecl()); |
121 } else { | 122 } else { |
122 ReachedNS = true; | 123 ReachedNS = true; |
123 auto *NSD = llvm::cast<NamespaceDecl>(CurContext); | 124 auto *NSD = llvm::cast<NamespaceDecl>(CurContext); |
256 if (const TypeSourceInfo *TSI = Cls->getTypeAsWritten()) { | 257 if (const TypeSourceInfo *TSI = Cls->getTypeAsWritten()) { |
257 // ClassTemplateSpecializationDecls do not contain | 258 // ClassTemplateSpecializationDecls do not contain |
258 // TemplateArgumentTypeLocs, they only have TemplateArgumentTypes. So we | 259 // TemplateArgumentTypeLocs, they only have TemplateArgumentTypes. So we |
259 // create a new argument location list from TypeSourceInfo. | 260 // create a new argument location list from TypeSourceInfo. |
260 auto STL = TSI->getTypeLoc().getAs<TemplateSpecializationTypeLoc>(); | 261 auto STL = TSI->getTypeLoc().getAs<TemplateSpecializationTypeLoc>(); |
261 llvm::SmallVector<TemplateArgumentLoc, 8> ArgLocs; | 262 llvm::SmallVector<TemplateArgumentLoc> ArgLocs; |
262 ArgLocs.reserve(STL.getNumArgs()); | 263 ArgLocs.reserve(STL.getNumArgs()); |
263 for (unsigned I = 0; I < STL.getNumArgs(); ++I) | 264 for (unsigned I = 0; I < STL.getNumArgs(); ++I) |
264 ArgLocs.push_back(STL.getArgLoc(I)); | 265 ArgLocs.push_back(STL.getArgLoc(I)); |
265 printTemplateArgumentList(OS, ArgLocs, Policy); | 266 printTemplateArgumentList(OS, ArgLocs, Policy); |
266 } else { | 267 } else { |
280 if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) | 281 if (!NS->isAnonymousNamespace() && !NS->isInlineNamespace()) |
281 return printQualifiedName(*NS) + "::"; | 282 return printQualifiedName(*NS) + "::"; |
282 return ""; | 283 return ""; |
283 } | 284 } |
284 | 285 |
285 llvm::Optional<SymbolID> getSymbolID(const Decl *D) { | 286 static llvm::StringRef |
287 getNameOrErrForObjCInterface(const ObjCInterfaceDecl *ID) { | |
288 return ID ? ID->getName() : "<<error-type>>"; | |
289 } | |
290 | |
291 std::string printObjCMethod(const ObjCMethodDecl &Method) { | |
292 std::string Name; | |
293 llvm::raw_string_ostream OS(Name); | |
294 | |
295 OS << (Method.isInstanceMethod() ? '-' : '+') << '['; | |
296 | |
297 // Should always be true. | |
298 if (const ObjCContainerDecl *C = | |
299 dyn_cast<ObjCContainerDecl>(Method.getDeclContext())) | |
300 OS << printObjCContainer(*C); | |
301 | |
302 Method.getSelector().print(OS << ' '); | |
303 if (Method.isVariadic()) | |
304 OS << ", ..."; | |
305 | |
306 OS << ']'; | |
307 OS.flush(); | |
308 return Name; | |
309 } | |
310 | |
311 std::string printObjCContainer(const ObjCContainerDecl &C) { | |
312 if (const ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(&C)) { | |
313 std::string Name; | |
314 llvm::raw_string_ostream OS(Name); | |
315 const ObjCInterfaceDecl *Class = Category->getClassInterface(); | |
316 OS << getNameOrErrForObjCInterface(Class) << '(' << Category->getName() | |
317 << ')'; | |
318 OS.flush(); | |
319 return Name; | |
320 } | |
321 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(&C)) { | |
322 std::string Name; | |
323 llvm::raw_string_ostream OS(Name); | |
324 const ObjCInterfaceDecl *Class = CID->getClassInterface(); | |
325 OS << getNameOrErrForObjCInterface(Class) << '(' << CID->getName() << ')'; | |
326 OS.flush(); | |
327 return Name; | |
328 } | |
329 return C.getNameAsString(); | |
330 } | |
331 | |
332 SymbolID getSymbolID(const Decl *D) { | |
286 llvm::SmallString<128> USR; | 333 llvm::SmallString<128> USR; |
287 if (index::generateUSRForDecl(D, USR)) | 334 if (index::generateUSRForDecl(D, USR)) |
288 return None; | 335 return {}; |
289 return SymbolID(USR); | 336 return SymbolID(USR); |
290 } | 337 } |
291 | 338 |
292 llvm::Optional<SymbolID> getSymbolID(const llvm::StringRef MacroName, | 339 SymbolID getSymbolID(const llvm::StringRef MacroName, const MacroInfo *MI, |
293 const MacroInfo *MI, | 340 const SourceManager &SM) { |
294 const SourceManager &SM) { | |
295 if (MI == nullptr) | 341 if (MI == nullptr) |
296 return None; | 342 return {}; |
297 llvm::SmallString<128> USR; | 343 llvm::SmallString<128> USR; |
298 if (index::generateUSRForMacro(MacroName, MI->getDefinitionLoc(), SM, USR)) | 344 if (index::generateUSRForMacro(MacroName, MI->getDefinitionLoc(), SM, USR)) |
299 return None; | 345 return {}; |
300 return SymbolID(USR); | 346 return SymbolID(USR); |
301 } | 347 } |
302 | 348 |
303 // FIXME: This should be handled while printing underlying decls instead. | |
304 std::string printType(const QualType QT, const DeclContext &CurContext) { | 349 std::string printType(const QualType QT, const DeclContext &CurContext) { |
305 std::string Result; | 350 std::string Result; |
306 llvm::raw_string_ostream OS(Result); | 351 llvm::raw_string_ostream OS(Result); |
307 auto Decls = explicitReferenceTargets( | |
308 ast_type_traits::DynTypedNode::create(QT), DeclRelation::Alias); | |
309 if (!Decls.empty()) | |
310 OS << getQualification(CurContext.getParentASTContext(), &CurContext, | |
311 Decls.front(), | |
312 /*VisibleNamespaces=*/llvm::ArrayRef<std::string>{}); | |
313 PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy()); | 352 PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy()); |
314 PP.SuppressScope = true; | |
315 PP.SuppressTagKeyword = true; | 353 PP.SuppressTagKeyword = true; |
354 PP.SuppressUnwrittenScope = true; | |
355 | |
356 class PrintCB : public PrintingCallbacks { | |
357 public: | |
358 PrintCB(const DeclContext *CurContext) : CurContext(CurContext) {} | |
359 virtual ~PrintCB() {} | |
360 virtual bool isScopeVisible(const DeclContext *DC) const override { | |
361 return DC->Encloses(CurContext); | |
362 } | |
363 | |
364 private: | |
365 const DeclContext *CurContext; | |
366 }; | |
367 PrintCB PCB(&CurContext); | |
368 PP.Callbacks = &PCB; | |
369 | |
316 QT.print(OS, PP); | 370 QT.print(OS, PP); |
317 return OS.str(); | 371 return OS.str(); |
318 } | 372 } |
319 | 373 |
320 QualType declaredType(const TypeDecl *D) { | 374 QualType declaredType(const TypeDecl *D) { |
349 if (!D->getTypeSourceInfo() || | 403 if (!D->getTypeSourceInfo() || |
350 D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation) | 404 D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation) |
351 return true; | 405 return true; |
352 | 406 |
353 if (auto *AT = D->getType()->getContainedAutoType()) { | 407 if (auto *AT = D->getType()->getContainedAutoType()) { |
354 if (!AT->getDeducedType().isNull()) | 408 DeducedType = AT->desugar(); |
355 DeducedType = AT->getDeducedType(); | |
356 } | 409 } |
357 return true; | 410 return true; |
358 } | 411 } |
359 | 412 |
360 // Handle auto return types: | 413 // Handle auto return types: |
367 if (!D->getTypeSourceInfo()) | 420 if (!D->getTypeSourceInfo()) |
368 return true; | 421 return true; |
369 // Loc of auto in return type (c++14). | 422 // Loc of auto in return type (c++14). |
370 auto CurLoc = D->getReturnTypeSourceRange().getBegin(); | 423 auto CurLoc = D->getReturnTypeSourceRange().getBegin(); |
371 // Loc of "auto" in operator auto() | 424 // Loc of "auto" in operator auto() |
372 if (CurLoc.isInvalid() && dyn_cast<CXXConversionDecl>(D)) | 425 if (CurLoc.isInvalid() && isa<CXXConversionDecl>(D)) |
373 CurLoc = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); | 426 CurLoc = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
374 // Loc of "auto" in function with trailing return type (c++11). | 427 // Loc of "auto" in function with trailing return type (c++11). |
375 if (CurLoc.isInvalid()) | 428 if (CurLoc.isInvalid()) |
376 CurLoc = D->getSourceRange().getBegin(); | 429 CurLoc = D->getSourceRange().getBegin(); |
377 if (CurLoc != SearchedLocation) | 430 if (CurLoc != SearchedLocation) |
469 // If that's not deduced yet, deducing it may change the linkage. | 522 // If that's not deduced yet, deducing it may change the linkage. |
470 auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D); | 523 auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D); |
471 return VD && !VD->getType().isNull() && VD->getType()->isUndeducedType(); | 524 return VD && !VD->getType().isNull() && VD->getType()->isUndeducedType(); |
472 } | 525 } |
473 | 526 |
527 bool isDeeplyNested(const Decl *D, unsigned MaxDepth) { | |
528 size_t ContextDepth = 0; | |
529 for (auto *Ctx = D->getDeclContext(); Ctx && !Ctx->isTranslationUnit(); | |
530 Ctx = Ctx->getParent()) { | |
531 if (++ContextDepth == MaxDepth) | |
532 return true; | |
533 } | |
534 return false; | |
535 } | |
474 } // namespace clangd | 536 } // namespace clangd |
475 } // namespace clang | 537 } // namespace clang |