Skip to content

Commit

Permalink
[flang][driver] Add -fdebug-dump-all
Browse files Browse the repository at this point in the history
The new option will run the semantic checks and then dump the parse tree
and all the symbols. This is equivalent to running the driver twice,
once with `-fdebug-dump-parse-tree` and then with
the `-fdebug-dump-symbols` action flag.

Currently we wouldn't be able to achieve the same by simply running:
```
flang-new -fc1 -fdebug-dump-parse-tree -fdebug-dump-symbols <input-file>
```
That's because the new driver will only run one frontend action per
invocation (both of the flags used here are action flags). Diverging
from this design would lead to costly compromises and it's best avoided.

We may want to consider re-designing our debugging actions (and action
options) in the future so that there's more code re-use. For now, I'm
focusing on making sure that we support all the major cases requested by
our users.

Differential Revision: https://reviews.llvm.org/D104305
  • Loading branch information
banach-space committed Jun 16, 2021
1 parent cff2155 commit a6be6e3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4523,6 +4523,8 @@ def fdebug_dump_parse_tree_no_sema : Flag<["-"], "fdebug-dump-parse-tree-no-sema
HelpText<"Dump the parse tree (skips the semantic checks)">,
DocBrief<[{Run the Parser and then output the parse tree. Semantic
checks are disabled.}]>;
def fdebug_dump_all : Flag<["-"], "fdebug-dump-all">, Group<Action_Group>,
HelpText<"Dump symbols and the parse tree after the semantic checks">;
def fdebug_dump_provenance : Flag<["-"], "fdebug-dump-provenance">, Group<Action_Group>,
HelpText<"Dump provenance">;
def fdebug_dump_parsing_log : Flag<["-"], "fdebug-dump-parsing-log">, Group<Action_Group>,
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Frontend/FrontendActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class DebugDumpParseTreeAction : public PrescanAndSemaAction {
void ExecuteAction() override;
};

class DebugDumpAllAction : public PrescanAndSemaAction {
void ExecuteAction() override;
};

class DebugPreFIRTreeAction : public PrescanAndSemaAction {
void ExecuteAction() override;
};
Expand Down
3 changes: 3 additions & 0 deletions flang/include/flang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ enum ActionKind {
/// Parse, run semantics and then output the parse tree
DebugDumpParseTree,

/// Parse, run semantics and then output the parse tree and symbols
DebugDumpAll,

/// Parse and then output the parse tree, skip the semantic checks
DebugDumpParseTreeNoSema,

Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ static bool ParseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
case clang::driver::options::OPT_fdebug_dump_parse_tree:
opts.programAction_ = DebugDumpParseTree;
break;
case clang::driver::options::OPT_fdebug_dump_all:
opts.programAction_ = DebugDumpAll;
break;
case clang::driver::options::OPT_fdebug_dump_parse_tree_no_sema:
opts.programAction_ = DebugDumpParseTreeNoSema;
break;
Expand Down
36 changes: 36 additions & 0 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,42 @@ void DebugDumpSymbolsAction::ExecuteAction() {
semantics.DumpSymbols(llvm::outs());
}

void DebugDumpAllAction::ExecuteAction() {
CompilerInstance &ci = this->instance();

// Dump parse tree
auto &parseTree{instance().parsing().parseTree()};
Fortran::parser::AnalyzedObjectsAsFortran asFortran =
Fortran::frontend::getBasicAsFortran();
llvm::outs() << "========================";
llvm::outs() << " Flang: parse tree dump ";
llvm::outs() << "========================\n";
Fortran::parser::DumpTree(llvm::outs(), parseTree, &asFortran);

auto &semantics = this->semantics();
auto tables{Fortran::semantics::BuildRuntimeDerivedTypeTables(
instance().invocation().semanticsContext())};
// The runtime derived type information table builder may find and report
// semantic errors. So it is important that we report them _after_
// BuildRuntimeDerivedTypeTables is run.
reportFatalSemanticErrors(
semantics, this->instance().diagnostics(), GetCurrentFileOrBufferName());

if (!tables.schemata) {
unsigned DiagID =
ci.diagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error,
"could not find module file for __fortran_type_info");
ci.diagnostics().Report(DiagID);
llvm::errs() << "\n";
}

// Dump symbols
llvm::outs() << "=====================";
llvm::outs() << " Flang: symbols dump ";
llvm::outs() << "=====================\n";
semantics.DumpSymbols(llvm::outs());
}

void DebugDumpParseTreeNoSemaAction::ExecuteAction() {
auto &parseTree{instance().parsing().parseTree()};
Fortran::parser::AnalyzedObjectsAsFortran asFortran =
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ static std::unique_ptr<FrontendAction> CreateFrontendBaseAction(
case DebugDumpParseTreeNoSema:
return std::make_unique<DebugDumpParseTreeNoSemaAction>();
break;
case DebugDumpAll:
return std::make_unique<DebugDumpAllAction>();
break;
case DebugDumpProvenance:
return std::make_unique<DebugDumpProvenanceAction>();
break;
Expand Down
1 change: 1 addition & 0 deletions flang/test/Driver/driver-help.f90
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
! HELP-FC1-NEXT: -falternative-parameter-statement
! HELP-FC1-NEXT: Enable the old style PARAMETER statement
! HELP-FC1-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! HELP-FC1-NEXT: -fdebug-dump-all Dump symbols and the parse tree after the semantic checks
! HELP-FC1-NEXT: -fdebug-dump-parse-tree-no-sema
! HELP-FC1-NEXT: Dump the parse tree (skips the semantic checks)
! HELP-FC1-NEXT: -fdebug-dump-parse-tree Dump the parse tree
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Driver/dump-all.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
!----------
! RUN lines
!----------
! RUN: %flang_fc1 -fdebug-dump-all %s 2>&1 | FileCheck %s

!----------------
! EXPECTED OUTPUT
!----------------
! CHECK: Flang: parse tree dump
! CHECK: Flang: symbols dump

!-------
! INPUT
!-------
parameter(i=1)
integer :: j
end program

0 comments on commit a6be6e3

Please sign in to comment.