Example #1
0
CompilationInfoForFile CompilationDatabase::GetCompilationInfoForFile(
    const std::string &path_to_file ) {
    ReleaseGil unlock;
    CompilationInfoForFile info;

    if ( !is_loaded_ )
        return info;

    lock_guard< mutex > lock( compilation_database_mutex_ );

    CompileCommandsWrap commands(
        clang_CompilationDatabase_getCompileCommands(
            compilation_database_,
            path_to_file.c_str() ), clang_CompileCommands_dispose );

    uint num_commands = clang_CompileCommands_getSize( commands.get() );

    if ( num_commands < 1 ) {
        return info;
    }

    // We always pick the first command offered
    CXCompileCommand command = clang_CompileCommands_getCommand(
                                   commands.get(),
                                   0 );

    info.compiler_working_dir_ = CXStringToString(
                                     clang_CompileCommand_getDirectory( command ) );

    uint num_flags = clang_CompileCommand_getNumArgs( command );
    info.compiler_flags_.reserve( num_flags );

    for ( uint i = 0; i < num_flags; ++i ) {
        info.compiler_flags_.push_back(
            CXStringToString( clang_CompileCommand_getArg( command, i ) ) );
    }

    return info;
}
Example #2
0
void Irony::getCompileOptions(const std::string &buildDir,
                              const std::string &file) const {
#if !(HAS_COMPILATION_DATABASE)

  (void)buildDir;
  (void)file;

  std::cout << "nil\n";
  return;

#else
  CXCompilationDatabase_Error error;
  CXCompilationDatabase db =
      clang_CompilationDatabase_fromDirectory(buildDir.c_str(), &error);

  switch (error) {
  case CXCompilationDatabase_CanNotLoadDatabase:
    std::clog << "I: could not load compilation database in '" << buildDir
              << "'\n";
    std::cout << "nil\n";
    return;

  case CXCompilationDatabase_NoError:
    break;
  }

  CXCompileCommands compileCommands =
      clang_CompilationDatabase_getCompileCommands(db, file.c_str());

  std::cout << "(\n";

  for (unsigned i = 0, numCompileCommands =
                           clang_CompileCommands_getSize(compileCommands);
       i < numCompileCommands; ++i) {
    CXCompileCommand compileCommand =
        clang_CompileCommands_getCommand(compileCommands, i);

    std::cout << "("
              << "(";
    for (unsigned j = 0,
                  numArgs = clang_CompileCommand_getNumArgs(compileCommand);
         j < numArgs; ++j) {
      CXString arg = clang_CompileCommand_getArg(compileCommand, j);
      std::cout << support::quoted(clang_getCString(arg)) << " ";
      clang_disposeString(arg);
    }

    std::cout << ")"
              << " . ";

    CXString directory = clang_CompileCommand_getDirectory(compileCommand);
    std::cout << support::quoted(clang_getCString(directory));
    clang_disposeString(directory);

    std::cout << ")\n";
  }

  std::cout << ")\n";

  clang_CompileCommands_dispose(compileCommands);
  clang_CompilationDatabase_dispose(db);
#endif
}