Skip to content

Add "-M" and "-MM" command line options to dump the dependencies of t… #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/frontend/cxx/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ std::vector<CLIOptionDescr> options{
{"-dM", "Print macro definitions in -E mode instead of normal output",
&CLI::opt_dM},

{"-M", "Print dependencies of the main source file",
&CLI::opt_M},

{"-MM", "Print dependencies of the main source file, excluding system path",
&CLI::opt_MM},

{"-S", "Only run preprocess and compilation steps", &CLI::opt_S,
CLIOptionVisibility::kExperimental},

Expand Down
2 changes: 2 additions & 0 deletions src/frontend/cxx/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class CLI {
bool opt_ast_dump = false;
bool opt_ir_dump = false;
bool opt_dM = false;
bool opt_MM = false;
bool opt_M = false;
bool opt_dump_symbols = false;
bool opt_dump_tokens = false;
bool opt_E = false;
Expand Down
8 changes: 7 additions & 1 deletion src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
}

if (auto source = readAll(fileName)) {
if (cli.opt_E && !cli.opt_dM) {
if (cli.opt_E && !(cli.opt_dM || cli.opt_M || cli.opt_MM)) {
preprocesor->preprocess(std::move(*source), fileName, output);
shouldExit = true;
} else {
Expand All @@ -247,6 +247,12 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
if (cli.opt_dM) {
preprocesor->printMacros(output);
shouldExit = true;
} else if (cli.opt_M) {
preprocesor->printDependencies(output, false);
shouldExit = true;
} else if (cli.opt_MM) {
preprocesor->printDependencies(output, true);
shouldExit = true;
} else if (cli.opt_dump_tokens) {
dumpTokens(cli, unit, output);
shouldExit = true;
Expand Down
21 changes: 21 additions & 0 deletions src/parser/cxx/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,27 @@ void Preprocessor::printMacros(std::ostream &out) const {
}
}

void Preprocessor::printDependencies(std::ostream &out, bool noSysPath) const {
if (d->sourceFiles_.size() == 0) return;
const auto p = fs::path(d->sourceFiles_[0]->fileName);
const std::string stem = p.stem();
out << cxx::format("{}.o:", stem);
for (const auto &sourceFile : d->sourceFiles_) {
if (noSysPath) {
bool isSysPath = false;
for (const auto &sysPath : d->systemIncludePaths_) {
if (sourceFile->fileName.find(sysPath) == 0) {
isSysPath = true;
break;
}
}
if (isSysPath) continue;
}
out << cxx::format(" \\\n {}", sourceFile->fileName);
}
out << "\n";
}

void Preprocessor::getTokenStartPosition(const Token &token, unsigned *line,
unsigned *column,
std::string_view *fileName) const {
Expand Down
2 changes: 2 additions & 0 deletions src/parser/cxx/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class Preprocessor {

void printMacros(std::ostream &out) const;

void printDependencies(std::ostream &out, bool noSysPath) const;

void getTokenStartPosition(const Token &token, unsigned *line,
unsigned *column,
std::string_view *fileName) const;
Expand Down