【问题标题】:how to create a virtual file in clang for codecompletion如何在 clang 中创建虚拟文件以完成代码补全
【发布时间】:2016-05-23 10:22:45
【问题描述】:

我正在尝试为 clang 中的代码完成创建虚拟文件。不幸的是,我的应用程序段错误。 我有以下设置:

auto createVirtualFile = [](
  clang::CompilerInstance& ci,
  std::string name,
  llvm::StringRef input
) {
  std::unique_ptr<llvm::MemoryBuffer>
    MB(llvm::MemoryBuffer::getMemBuffer(input, name));
  return std::move(MB);
};

创建文件后,我会设置一个 CodeCompletConsumer:

auto setupCodeComplete = [](
  clang::CompilerInstance& ci,
  std::string File,
  int Line,
  int Column
) {
  auto& F = ci.getFrontendOpts();
  F.CodeCompletionAt.FileName = File;
  F.CodeCompletionAt.Line = Line;
  F.CodeCompletionAt.Column = Column;
  clang::FrontendInputFile FrontFile(File, clang::IK_CXX);
  //F.Inputs.push_back(FrontFile);
  ci.createCodeCompletionConsumer();
  return FrontFile;
};

我通过以下方式调用这两个函数并执行仅语法操作:

auto runCodeCompleteAt = [] (
  clang::CompilerInstance& ci,
  std::string Filename,
  std::string Code,
  int Line,
  int Column
) {
  auto fid = createVirtualFile(ci, Filename, Code);
  auto File = setupCodeComplete(ci, Filename, Line, Column);
  clang::SyntaxOnlyAction Act;
  if (Act.BeginSourceFile(ci, File )) {
    Act.Execute(); // segfault
    Act.EndSourceFile();
  }
};

auto runExample = [](auto& ci){
  runCodeCompleterAt(ci, "test.cpp", "std::cou", 1, 7);
}

感谢任何提示。

【问题讨论】:

标签: c++ autocomplete clang llvm code-completion


【解决方案1】:

问题是虽然你可以在clang::FrontendInputFile中添加llvm::MemoryBuffer,但clang::SyntaxOnlyAction不接受这样的输入,会导致断言失败。正如我在previous answer 中所说,您可能想在lib/Frontend/ASTUnit.cpp 中查看ASTUnit::CodeComplete

其实你可以使用remappedFiles来链接一个文件名和一个缓冲区:

int main()
{
    unsigned Line = 2;
    unsigned Column = 13;
    clang::CompilerInstance ci;
    llvm::StringRef codeStr("void MyFoo() {}\nint main() {My return 0}");

    // Just a dummy filename, the file even doesnt' exist.
    std::string Filename("virtualFile.cpp");
    std::unique_ptr<llvm::MemoryBuffer> MB(llvm::MemoryBuffer::getMemBuffer(codeStr, Filename));

    // Create diagnostics and target
    ci.createDiagnostics();
    std::shared_ptr<clang::TargetOptions> to = std::make_shared<clang::TargetOptions>();
    // clang -v
    //to->Triple = "x86_64-pc-win32";
    //to->Triple = "x86_64-apple-darwin";
    to->Triple = "x86_64-apple-darwin15";
    ci.setTarget(clang::TargetInfo::CreateTargetInfo(ci.getDiagnostics(), to));

    // Create language options
    clang::LangOptions &lo = ci.getLangOpts();
    lo.CPlusPlus = true;
    lo.CPlusPlus11 = true;

    // Create code complete options
    clang::CodeCompleteOptions cco;
    cco.IncludeMacros = 0;
    cco.IncludeCodePatterns = 1;
    cco.IncludeGlobals = 1;
    cco.IncludeBriefComments = 1;

    // Set up the callback, I will go back to this callback class later
    auto pCustomCodeCompleteConsumer = new CustomCodeCompleteConsumer(cco);
    ci.setCodeCompletionConsumer(pCustomCodeCompleteConsumer);

    // Set up code complete postions & file
    // Until now I didn't find a way to pass in a string rather than a file name
    clang::FrontendOptions& frontendOpts = ci.getFrontendOpts();
    frontendOpts.CodeCompletionAt.FileName = Filename;
    frontendOpts.CodeCompletionAt.Line = Line;
    frontendOpts.CodeCompletionAt.Column = Column;
    frontendOpts.Inputs.push_back(clang::FrontendInputFile(Filename, clang::InputKind::IK_CXX));

    clang::PreprocessorOptions& preprocessorOpts = ci.getPreprocessorOpts();
    preprocessorOpts.clearRemappedFiles();
    preprocessorOpts.addRemappedFile(Filename, MB.release());

    // Execute
    clang::SyntaxOnlyAction Act;
    if (Act.BeginSourceFile(ci, ci.getFrontendOpts().Inputs[0])) {
        Act.Execute();
        Act.EndSourceFile();
    }
}

输出包含COMPLETION: 50 Decl : MyFoo : [#void#]MyFoo()

我从代码段得到这个答案

  PreprocessorOpts.clearRemappedFiles();
  PreprocessorOpts.RetainRemappedFileBuffers = true;
  for (const auto &RemappedFile : RemappedFiles) {
    PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second);
    OwnedBuffers.push_back(RemappedFile.second);
  }

ASTUnit::CodeComplete.

我相信您可能想阅读AugmentedCodeCompleteConsumer 以利用这些完成提示。

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 2016-09-29
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2016-11-04
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多