Author: kadircet
Date: Fri Apr 12 03:09:14 2019
New Revision: 358272
URL: http://llvm.org/viewvc/llvm-project?rev=358272=rev
Log:
[clangd] Print template arguments helper
Summary:
Prepares ground for printing template arguments as written in the
source code, part of re-landing rC356541 with D59599 applied.
Reviewers: ioeric, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59639
Added:
clang-tools-extra/trunk/unittests/clangd/PrintASTTests.cpp
Modified:
clang-tools-extra/trunk/clangd/AST.cpp
clang-tools-extra/trunk/clangd/AST.h
clang-tools-extra/trunk/unittests/clangd/CMakeLists.txt
clang-tools-extra/trunk/unittests/clangd/FindSymbolsTests.cpp
Modified: clang-tools-extra/trunk/clangd/AST.cpp
URL:
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=358272=358271=358272=diff
==
--- clang-tools-extra/trunk/clangd/AST.cpp (original)
+++ clang-tools-extra/trunk/clangd/AST.cpp Fri Apr 12 03:09:14 2019
@@ -11,15 +11,37 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/TemplateBase.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Index/USRGeneration.h"
+#include "llvm/ADT/Optional.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ScopedPrinter.h"
+#include "llvm/Support/raw_ostream.h"
namespace clang {
namespace clangd {
+namespace {
+llvm::Optional>
+getTemplateSpecializationArgLocs(const NamedDecl ) {
+ if (auto *Func = llvm::dyn_cast()) {
+if (const ASTTemplateArgumentListInfo *Args =
+Func->getTemplateSpecializationArgsAsWritten())
+ return Args->arguments();
+ } else if (auto *Cls =
+ llvm::dyn_cast()) {
+if (auto *Args = Cls->getTemplateArgsAsWritten())
+ return Args->arguments();
+ } else if (auto *Var = llvm::dyn_cast())
+return Var->getTemplateArgsInfo().arguments();
+ // We return None for ClassTemplateSpecializationDecls because it does not
+ // contain TemplateArgumentLoc information.
+ return llvm::None;
+}
+} // namespace
+
// Returns true if the complete name of decl \p D is spelled in the source
code.
// This is not the case for:
// * symbols formed via macro concatenation, the spelling location will
@@ -65,17 +87,6 @@ std::string printQualifiedName(const Nam
return QName;
}
-static const TemplateArgumentList *
-getTemplateSpecializationArgs(const NamedDecl ) {
- if (auto *Func = llvm::dyn_cast())
-return Func->getTemplateSpecializationArgs();
- if (auto *Cls = llvm::dyn_cast())
-return >getTemplateInstantiationArgs();
- if (auto *Var = llvm::dyn_cast())
-return >getTemplateInstantiationArgs();
- return nullptr;
-}
-
std::string printName(const ASTContext , const NamedDecl ) {
std::string Name;
llvm::raw_string_ostream Out(Name);
@@ -90,9 +101,7 @@ std::string printName(const ASTContext &
}
ND.getDeclName().print(Out, PP);
if (!Out.str().empty()) {
-// FIXME(ibiryukov): do not show args not explicitly written by the user.
-if (auto *ArgList = getTemplateSpecializationArgs(ND))
- printTemplateArgumentList(Out, ArgList->asArray(), PP);
+Out << printTemplateSpecializationArgs(ND);
return Out.str();
}
// The name was empty, so present an anonymous entity.
@@ -105,6 +114,35 @@ std::string printName(const ASTContext &
return "(anonymous)";
}
+std::string printTemplateSpecializationArgs(const NamedDecl ) {
+ std::string TemplateArgs;
+ llvm::raw_string_ostream OS(TemplateArgs);
+ PrintingPolicy Policy(ND.getASTContext().getLangOpts());
+ if (llvm::Optional> Args =
+ getTemplateSpecializationArgLocs(ND)) {
+printTemplateArgumentList(OS, *Args, Policy);
+ } else if (auto *Cls = llvm::dyn_cast())
{
+if (const TypeSourceInfo *TSI = Cls->getTypeAsWritten()) {
+ // ClassTemplateSpecializationDecls do not contain
+ // TemplateArgumentTypeLocs, they only have TemplateArgumentTypes. So we
+ // create a new argument location list from TypeSourceInfo.
+ auto STL = TSI->getTypeLoc().getAs();
+ llvm::SmallVector ArgLocs;
+ ArgLocs.reserve(STL.getNumArgs());
+ for (unsigned I = 0; I < STL.getNumArgs(); ++I)
+ArgLocs.push_back(STL.getArgLoc(I));
+ printTemplateArgumentList(OS, ArgLocs, Policy);
+} else {
+ // FIXME: Fix cases when getTypeAsWritten returns null inside clang AST,
+ // e.g. friend decls. Currently we fallback to Template Arguments without
+ // location information.
+ printTemplateArgumentList(OS, Cls->getTemplateArgs().asArray(), Policy);
+}
+ }
+ OS.flush();
+ return TemplateArgs;
+}
+
std::string printNamespaceScope(const DeclContext ) {
for (const auto *Ctx = Ctx != nullptr; Ctx = Ctx->getParent())
if