[clang] [TEST-ONLY] Make sure clang-extdef-mapping don't infer database (PR #195196)

Arseniy Zaostrovnykh via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 30 22:13:34 PDT 2026


https://github.com/necto approved this pull request.

This is some surprising gotcha. I had to ask LLM to explain how `--` stops the comp-DB lookup (`clang-extdef-map --help` does not mention it at all). I approve of this PR as an improvement. An extra mile could be to document this behavior somewhere (in the `clang-extdef-map --help`?).

Here is the explanation produced by Claude:

# How `--` Affects `clang-extdef-map` Compilation Database Lookup

## Argument Parsing Flow

`clang-extdef-mapping` uses `CommonOptionsParser::init` (`clang/lib/Tooling/CommonOptionsParser.cpp`), which runs two phases in sequence:

**Phase 1** — try to build a `FixedCompilationDatabase` from `--`:

```cpp
// CommonOptionsParser.cpp:110-111
Compilations =
    FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage);
```

**Phase 2** — if Phase 1 returned null, auto-detect from the filesystem:

```cpp
// CommonOptionsParser.cpp:127-141
if (!Compilations) {
    if (!BuildPath.empty()) {
      Compilations =
          CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage);
    } else {
      Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0],
                                                               ErrorMessage);
    }
    // ...fallback to empty FixedCompilationDatabase if nothing found
}
```

## The `--` Detection Logic

`FixedCompilationDatabase::loadFromCommandLine` (`clang/lib/Tooling/CompilationDatabase.cpp:317-335`):

```cpp
const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
if (DoubleDash == Argv + Argc)
  return nullptr;                          // no "--" found -> null -> Phase 2 runs
std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
Argc = DoubleDash - Argv;                 // truncate argv before "--"
// ...
return std::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs);
```

If `--` is present, it returns a non-null `FixedCompilationDatabase` built from flags after `--`. This **short-circuits** Phase 2 entirely — no filesystem search happens.


https://github.com/llvm/llvm-project/pull/195196


More information about the cfe-commits mailing list