c++ - Clang 如何编译模板实例化?

标签 c++ templates clang llvm

我正在使用 Clang 作为一个库来编译一些模板代码:

template<typename T>
T getSevenTemplated() {
  return 7;
}

int getSeven() {
  return getSevenTemplated<int>();
}

不幸的是,编译后的 LLVM IR 实际上并不包含 getSevenTemplated<int> 的实现。 :

; ModuleID = './test.cpp'
source_filename = "./test.cpp"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"

; Function Attrs: ssp uwtable
define i32 @_Z8getSevenv() #0 {
entry:
  %call = call i32 @_Z17getSevenTemplatedIiET_v()
  ret i32 %call
}

declare i32 @_Z17getSevenTemplatedIiET_v() #1

attributes #0 = { ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 5.0.0 (trunk 292778)"}

这是我用来生成 LLVM 模块的代码:

auto* context = new llvm::LLVMContext(); // TODO: Fix leak 

auto codeGenerator =
  std::shared_ptr<clang::CodeGenerator>(
    clang::CreateLLVMCodeGen(
      compilerInstance.getDiagnostics(), 
      filePath,
      compilerInstance.getHeaderSearchOpts(), 
      preprocessor.getPreprocessorOpts(),
      compilerInstance.getCodeGenOpts(), 
      *context, 
      nullptr));

codeGenerator->Initialize(compilerInstance.getASTContext());

// declGroups are found by calling ParseAST with a special ASTConsumer earlier
for (auto declGroup : declGroups) { 
  codeGenerator->HandleTopLevelDecl(declGroup);
}

codeGenerator->HandleTranslationUnit(compilerInstance.getASTContext());

为了调查原因,我查看了 the source-code我发现 CodeGenerator具有处理模板的特定逻辑:它似乎跳过了函数模板实例化!我假设它会处理生成的函数,但我不知道它是如何工作的。

我的问题是:

  1. 在高层次上,Clang 如何实例化模板并将它们传递给 ASTConsumer用于代码生成?
  2. 是否有任何特殊技巧可以为实例化模板生成函数体?我从上面的代码中遗漏了什么?

最佳答案

问题是我打开了增量解析。启用此设置后,ParseAST 函数不会调用 clang::Sema::ActOnEndOfTranslationUnit,这会触发模板的实例化。

诀窍是在处理完声明后添加此调用:

// This triggers the instantiation of templated functions
sema.ActOnEndOfTranslationUnit();

关于c++ - Clang 如何编译模板实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42001524/

相关文章:

c++ - 排序 vector 时运算符重载的编译错误

c++ - 类 std::out_of_range 在哪里完全定义?

c++ - 范围解析运算符和从属名称

c++ - 如何强制 clang 使用 llvm 汇编程序而不是系统?

c++ - LibClang clang_getArgType() 返回错误的类型

c++ - 从模板基类派生时找不到类型

C++错误:使用已删除的函数boost::asio::io_context::io_context

c++ - template< template <typename> something_else >,这是什么?

c++ - 函数模板作为参数

c++ - 如何在 clang 库中执行模板替换?