clang-tidy cant detect compile_commands.json

169 views Asked by At

Heres my compile_commands.json :

{ "directory": "/data/data/com.termux/files/home/cpulator", "file": "allocator.cc", "output": "allocator.o", "arguments": ["/data/data/com.termux/files/usr/bin/clang-16", "-xc++", "--sysroot=/data/data/com.termux/files", "events.cc", "-o", "events.o", "--target=arm-altera-eabi", "-nostdlib", "-Wall", "-Wextra", "-Wpedantic", "-c", "-O0", "-gdwarf-4", "-fno-exceptions", "-fno-rtti", "-fno-inline", "--target=armv4t-altera-unknown-eabi"]}

When I run clang-tidy allocator.cc, it acts as if it cant detect the compile_commands.json file.

Error message:

Error while trying to load a compilation database:
Could not auto-detect compilation database from directory "/data/data/com.termux/files/home/cpulator"
No compilation database found in /data/data/com.termux/files/home/cpulator or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Expected array.

How do I fix this?

1

There are 1 answers

1
Scott McPeak On BEST ANSWER

The error message:

json-compilation-database: Expected array.

comes from JSONCompilationDatabase.cpp:

bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
  llvm::yaml::document_iterator I = YAMLStream.begin();
  if (I == YAMLStream.end()) {
    ErrorMessage = "Error while parsing YAML.";
    return false;
  }
  llvm::yaml::Node *Root = I->getRoot();
  if (!Root) {
    ErrorMessage = "Error while parsing YAML.";
    return false;
  }
  auto *Array = dyn_cast<llvm::yaml::SequenceNode>(Root);
  if (!Array) {
    ErrorMessage = "Expected array.";        // <-------------------
    return false;
  }

It means the compilation_database.json file was found, but did not begin with the required square bracket character [.

The documentation of the compile_commands.json format explains (emphasis mine):

A compilation database is a JSON file, which consist of an array of “command objects” ...

To fix your compile_commands.json file, simply insert [ at the start and ] at the end.