c++ - Clang - 获取 SubstTemplateTypeParm 完整模板信息
问题描述
我正在遍历 clang AST,但是在遍历包含 clang::SubstTemplateTypeParmType 的 AST 声明的类型信息时遇到了问题。
给定以下用于 clang 工具的最小输入代码
#include <map>
template <typename K, typename V> using Map = std::map<K, V>;
using String = std::string;
using ParameterMap = Map<String, String>;
ParameterMap someFunc();
当通过ParameterMap
's 类型递归时,clang 说第一个Map
参数 arg, String
, 是 a clang::SubstTemplateTypeParmType
。String
如果我尝试通过去糖或获取替换类型(下面的代码)进一步递归以获取有关 的更多信息,则基础类型是 ofType::Record
和 is std::basic_string
。这对我来说是出乎意料的,因为我希望底层类型是模板专业化,比如basic_string<const char*, std::char_traits<const char*>, std::allocator<const char*>>
. 由于节点是一条记录,我可以得到地图包含一个std::basic_string
,但不能得到的模板信息basic_string
。basic_string
在这种情况下如何获取模板专业化信息?
case Type::SubstTemplateTypeParm:
{
auto substTemplateType = qualType->getAs<SubstTemplateTypeParmType>();
walkType(substTemplateType->getReplacementType());
return;
}
我了解发布最小可运行代码示例的要求,如下所示。然而,这需要一个 clang 工具依赖,没有预先构建的,所以这不是那么容易插入和运行。
路径是硬编码的,因此需要根据您的本地设置进行更新。这是 compile_commands 文件,它也有 3 个更新路径。编译器路径,和最后的文件路径两次。
[
{"directory":"F:/git/minRepro/","command":"\"C:/Program Files (x86)/compilers/clang.exe\" -Wall -isystem -g -std=c++14 -Wno-format -Wno-unneeded-internal-declaration -Werror F:/git/minRepro/exampleSource.cpp","file":"F:/git/minRepro/exampleSource.cpp"}
]
代码:
#pragma comment(lib,"Version.lib")
#include <clang/Tooling/JSONCompilationDatabase.h>
#include <clang/Tooling/Tooling.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/AST/Type.h>
#include <clang/AST/TemplateName.h>
#include <clang/AST/Decl.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/Frontend/CompilerInstance.h>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <string>
class AstWalker : public clang::SemaConsumer
{
public:
AstWalker(clang::ASTContext& context)
: m_context(context)
{}
virtual void HandleTranslationUnit(clang::ASTContext& context)
{
using namespace clang;
for (auto declaration : context.getTranslationUnitDecl()->decls())
{
const auto&sm = m_context.getSourceManager();
if (!declaration->getBeginLoc().isValid())
continue;
// Only walk declarations from our file.
if (!sm.isInMainFile(sm.getSpellingLoc(declaration->getBeginLoc())))
continue;
// Find functions, and inspect their return type.
auto nodeKind = declaration->getKind();
if (nodeKind == Decl::Function)
{
auto funcDecl = cast<FunctionDecl>(declaration);
// Check for and ignore built-in functions.
if (funcDecl->getBuiltinID() != 0)
break;
walkType(funcDecl->getReturnType());
break;
}
}
}
void walkType(const clang::QualType& qualType)
{
using namespace clang;
auto classType = qualType->getTypeClass();
switch (classType)
{
case Type::Typedef:
{
auto typedefType = qualType->getAs<TypedefType>();
walkType(typedefType->desugar());
return;
}
case Type::TemplateSpecialization:
{
auto templateSpecialization = qualType->getAs<TemplateSpecializationType>();
if (templateSpecialization->isTypeAlias())
{
walkType(templateSpecialization->getAliasedType());
return;
}
std::string templateType = templateSpecialization->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
std::cout << templateType << "<";
auto numArgs = templateSpecialization->getNumArgs();
for (unsigned int i = 0; i < numArgs; ++i)
{
if (i > 0)
std::cout << ", ";
const clang::TemplateArgument& templateArg = templateSpecialization->getArg(i);
if (templateArg.getKind() == clang::TemplateArgument::ArgKind::Type)
{
walkType(templateArg.getAsType());
}
}
std::cout << ">";
return;
}
case Type::Record:
{
const auto record = qualType->getAs<RecordType>();
std::string recordQualifiedName = record->getAsRecordDecl()->getQualifiedNameAsString();
std::cout << recordQualifiedName;
return;
}
case Type::Elaborated:
{
auto elaboratedType = qualType->getAs<ElaboratedType>();
walkType(elaboratedType->desugar());
return;
}
case Type::SubstTemplateTypeParm:
{
auto substTemplateType = qualType->getAs<SubstTemplateTypeParmType>();
walkType(substTemplateType->desugar());
//Also tried getReplacementType.
//walkType(substTemplateType->getReplacementType());
return;
}
}
}
private:
clang::ASTContext& m_context;
};
class ExampleAction : public clang::ASTFrontendAction
{
public:
ExampleAction() {}
virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler, llvm::StringRef inFile)
{
return std::unique_ptr<clang::ASTConsumer>(new AstWalker(compiler.getASTContext()));
}
};
int main(int argc, char **argv)
{
// Create the compilation database.
std::string errorOut;
std::unique_ptr<clang::tooling::JSONCompilationDatabase> compilationDatabase = clang::tooling::JSONCompilationDatabase::loadFromFile("F:/git/minRepro/compile_commands.json", errorOut, clang::tooling::JSONCommandLineSyntax::AutoDetect);
if (compilationDatabase == nullptr || !errorOut.empty())
{
std::cout << "[Error] Failed to load compilation database. Error=" << errorOut.c_str() << std::endl;
return false;
}
std::vector<std::string> headerFiles;
headerFiles.push_back("F:/git/minRepro/exampleSource.cpp");
clang::tooling::ClangTool tool(*compilationDatabase, llvm::ArrayRef<std::string>(headerFiles));
auto toolResult = tool.run(clang::tooling::newFrontendActionFactory<ExampleAction>().get());
if (toolResult == 1)
{
std::cout << "[Error] Error occurred. Check log. Aborting.\n";
assert(false);
return false;
}
}
解决方案
在某些情况下,模板信息嵌套在ClassTemplateSpecializationDecl
给定的RecordType
. 您可以通过将其RecordDecl
转换为ClassTemplateSpecializationDecl
.
const auto record = qualType->getAs<clang::RecordType>();
auto classTemplateSpecialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(record->getAsRecordDecl());
if (classTemplateSpecialization)
{
const auto& args = classTemplateSpecialization->getTemplateArgs();
for (unsigned int i = 0; i < args.size(); ++i)
{
const clang::TemplateArgument& templateArg = args[i];
// Read arg info as needed.
}
}
else
{
// Not a template specialization.
}
推荐阅读
- c# - 无法使用 C# 中的空格启动命令行
- python - 如何将值传递给 tkinter 页面?
- python - 从字典 Python 中初始化许多对象?
- image - NodeJS 发布多部分数据(图片上传)
- php - 我想在 kendo ui 网格上显示来自 mysql db 的数据,但是我看不到表格上的信息,而是看到带有列的空网格
- java - 无法使用多阶段 Docker 构建运行 Spring Boot 应用程序
- haskell - 使用数据构造函数过滤表
- python - 单击并在python中输入时如何使代码运行?
- html - 使用 HTML 对象标签查看 PDF 的最后一页或滚动到 PDF 的最后一页
- javascript - findOneandReplace 一直报错:“错误:替换文档不能包含原子操作符。”?