命名空间标准和 LLVM PassManagerInternal.h 文件

namespace std and LLVM PassManagerInternal.h file

我正在使用此示例探索使用 Flex、Bison 和 LLVM(所有的最新版本)编写编译器:http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/ Github 来源在那个 link 的最后一页。我无法编译它,我们将不胜感激。

对我来说不幸的是,解决方案并不明显。

错误如下:

In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
In file included from /usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:46:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:298:12: error: use of undeclared identifier 'make_unique'
    return make_unique<ResultModelT>(Pass.run(IR, AM));
           ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:298:24: error: unexpected type name 'ResultModelT': expected expression
    return make_unique<ResultModelT>(Pass.run(IR, AM));
                       ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:336:12: error: use of undeclared identifier 'make_unique'
    return make_unique<ResultModelT>(Pass.run(IR));
           ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManagerInternal.h:336:24: error: unexpected type name 'ResultModelT': expected expression
    return make_unique<ResultModelT>(Pass.run(IR));
                       ^
codegen.cpp:36:24: error: no matching conversion for functional-style cast from 'unique_ptr<llvm::Module>' to 'llvm::EngineBuilder'
        ExecutionEngine *ee = EngineBuilder( unique_ptr<Module>(module) ).create();
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:493:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion
      from 'unique_ptr<llvm::Module>' to 'llvm::EngineBuilder' for 1st argument
class EngineBuilder {
      ^
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:493:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion
      from 'unique_ptr<llvm::Module>' to 'const llvm::EngineBuilder' for 1st argument
/anaconda/include/llvm/ExecutionEngine/ExecutionEngine.h:525:3: note: candidate constructor not viable: no known conversion from 'unique_ptr<llvm::Module>'
      to 'llvm::Module *' for 1st argument
  EngineBuilder(Module *m) : M(m) {
  ^
codegen.cpp:131:49: warning: expression with side effects will be evaluated despite being used as an operand to 'typeid'
      [-Wpotentially-evaluated-expression]
                std::cout << "Generating code for " << typeid(**it).name() << endl;
                                                              ^
In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:207:22: error: no member named 'getName' in 'llvm::Module'
               << IR.getName() << "\n";
                  ~~ ^
codegen.cpp:30:5: note: in instantiation of member function 'llvm::PassManager<llvm::Module>::run' requested here
        pm.run(*module);
           ^
In file included from codegen.cpp:2:
In file included from ./codegen.h:8:
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:517:20: error: no member named 'getName' in 'llvm::Module'
             << IR.getName() << "\n";
                ~~ ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:372:28: note: in instantiation of member function
      'llvm::AnalysisManager<llvm::Module>::invalidateImpl' requested here
    return derived_this()->invalidateImpl(IR, std::move(PA));
                           ^
/usr/local/Cellar/llvm/3.8.1/include/llvm/IR/PassManager.h:217:22: note: in instantiation of member function
      'llvm::detail::AnalysisManagerBase<llvm::AnalysisManager<llvm::Module>, llvm::Module>::invalidate' requested here
        PassPA = AM->invalidate(IR, std::move(PassPA));
                     ^
codegen.cpp:30:5: note: in instantiation of member function 'llvm::PassManager<llvm::Module>::run' requested here
        pm.run(*module);
           ^

嗯,include/llvm/ADT/STLExtras.h中有llvm::make_unique。您没有说明您是基于 llvm 源编写自己的代码还是编译 llvm 或什么,但我假设 llvm::make_unique 正是出于这个原因来填补 C++11 的缺失部分。

此外,查看 tutorials 似乎到处都明确使用了 llvm::make_unique

更新

所以我成功地从 svn 构建了针对 LLVM 3.9.0 的给定示例。我不得不更改并修复对 getGlobalContext() 的所有调用,因为它在 LLVM 3.9.0 中消失了,但我没有遇到任何与 make_unique 相关的问题。然而,这个例子本身有点破。 codegen.h 缺少包含防护,codegen.cpp 缺少 #include "llvm/IR/LLVMContext.h" 的包含,即使它想使用旧的 getGlobalContext() 方法。我在 Windows 上使用 MSVC 2015 完成了此操作,您能否指定您的环境和编译器以便我可以尝试相同的操作?