gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:38:19 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com