Skip to content

Add argument to parse headers via a source's flags#58

Open
Steelskin wants to merge 1 commit into
compnerd:mainfrom
Steelskin:fabrice/add-cdb-source-parameter
Open

Add argument to parse headers via a source's flags#58
Steelskin wants to merge 1 commit into
compnerd:mainfrom
Steelskin:fabrice/add-cdb-source-parameter

Conversation

@Steelskin
Copy link
Copy Markdown

idt requires input to have an entry in the compilation database in order to be properly parsed. This is not always the case when parsing headers, depending on the project configuration.

This change adds --cdb-source=<path>. When set, idt looks up <path> in the compilation database and adjusts the command line to parse the positional argument with the same flags as <path>, assuming that the positional argument is a header.

Fixits are gated behind the is_in_main_tu() helper when --cdb-source is set to prevent spilling of fixits outside of the positional input. The helper is a no-op otherwise, preserving existing behavior.

idt requires input to have an entry in the compilation database in order
to be properly parsed. This is not always the case when parsing headers,
depending on the project configuration.

This change adds `--cdb-source=<path>`. When set, idt looks up `<path>`
in the compilation database and adjusts the command line to parse the
positional argument with the same flags as `<path>`, assuming that the
positional argument is a header.

Fixits are gated behind the `is_in_main_tu()` helper when `--cdb-source`
is set to prevent spilling of fixits outside of the positional input.
The helper is a no-op otherwise, preserving existing behavior.
Comment thread Sources/idt/idt.cc
llvm::cl::cat(idt::category));

llvm::cl::opt<std::string>
cdb_source("cdb-source",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be file (--file).

Comment thread Sources/idt/idt.cc
static void rewrite(clang::tooling::CompileCommand &command,
llvm::StringRef path) {
static constexpr std::array<llvm::StringRef, 5> kSourceExts{
".cpp", ".cc", ".cxx", ".c++", ".C"};
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
".cpp", ".cc", ".cxx", ".c++", ".C"};
".cpp", ".cc", ".cxx", ".c++", ".C",
};

Comment thread Sources/idt/idt.cc
Comment on lines +823 to +833
// Enforce `-x c++-header`. Replace any existing `-x <lang>` value, or
// insert `-x c++-header` immediately after argv[0].
auto x_it =
std::find(command.CommandLine.begin(), command.CommandLine.end(), "-x");
if (x_it != command.CommandLine.end() &&
std::next(x_it) != command.CommandLine.end()) {
*std::next(x_it) = "c++-header";
} else if (!command.CommandLine.empty()) {
command.CommandLine.insert(std::next(command.CommandLine.begin()),
{"-x", "c++-header"});
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an explanation as to why we are using C++ as the language. Also, why are we using c++header?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants