Skip to content

[flang] Intermix messages from parser and semantic analysis #90654

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

Merged
merged 1 commit into from
May 1, 2024
Merged
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
4 changes: 3 additions & 1 deletion flang/examples/FeatureList/FeatureList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,9 @@ class FeatureListAction : public PluginParseTreeAction {
}
}

bool beginSourceFileAction() override { return runPrescan() && runParse(); }
bool beginSourceFileAction() override {
return runPrescan() && runParse(/*emitMessages=*/true);
}
};

static FrontendPluginRegistry::Add<FeatureListAction> X(
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Frontend/FrontendAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class FrontendAction {
bool runPrescan();
// Parse the current input file. Return False if fatal errors are reported,
// True otherwise.
bool runParse();
bool runParse(bool emitMessages);
// Run semantic checks for the current input file. Return False if fatal
// errors are reported, True otherwise.
bool runSemanticChecks();
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Semantics/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class Semantics {
return context_.FindScope(where);
}
bool AnyFatalError() const { return context_.AnyFatalError(); }
void EmitMessages(llvm::raw_ostream &) const;
void EmitMessages(llvm::raw_ostream &);
void DumpSymbols(llvm::raw_ostream &);
void DumpSymbolsSources(llvm::raw_ostream &) const;

Expand Down
20 changes: 13 additions & 7 deletions flang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ bool FrontendAction::runPrescan() {
return !reportFatalScanningErrors();
}

bool FrontendAction::runParse() {
bool FrontendAction::runParse(bool emitMessages) {
CompilerInstance &ci = this->getInstance();

// Parse. In case of failure, report and return.
Expand All @@ -163,9 +163,11 @@ bool FrontendAction::runParse() {
return false;
}

// Report the diagnostics from getParsing
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());

if (emitMessages) {
// Report any non-fatal diagnostics from getParsing now rather than
// combining them with messages from semantics.
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
}
return true;
}

Expand All @@ -174,10 +176,14 @@ bool FrontendAction::runSemanticChecks() {
std::optional<parser::Program> &parseTree{ci.getParsing().parseTree()};
assert(parseTree && "Cannot run semantic checks without a parse tree!");

// Transfer any pending non-fatal messages from parsing to semantics
// so that they are merged and all printed in order.
auto &semanticsCtx{ci.getSemanticsContext()};
semanticsCtx.messages().Annex(std::move(ci.getParsing().messages()));

// Prepare semantics
ci.setSemantics(std::make_unique<Fortran::semantics::Semantics>(
ci.getSemanticsContext(), *parseTree,
ci.getInvocation().getDebugModuleDir()));
semanticsCtx, *parseTree, ci.getInvocation().getDebugModuleDir()));
auto &semantics = ci.getSemantics();

// Run semantic checks
Expand All @@ -187,7 +193,7 @@ bool FrontendAction::runSemanticChecks() {
return false;
}

// Report the diagnostics from the semantic checks
// Report the diagnostics from parsing and the semantic checks
semantics.EmitMessages(ci.getSemaOutputStream());

return true;
Expand Down
14 changes: 7 additions & 7 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ static bool saveMLIRTempFile(const CompilerInvocation &ci,
bool PrescanAction::beginSourceFileAction() { return runPrescan(); }

bool PrescanAndParseAction::beginSourceFileAction() {
return runPrescan() && runParse();
return runPrescan() && runParse(/*emitMessages=*/true);
}

bool PrescanAndSemaAction::beginSourceFileAction() {
return runPrescan() && runParse() && runSemanticChecks() &&
generateRtTypeTables();
return runPrescan() && runParse(/*emitMessages=*/false) &&
runSemanticChecks() && generateRtTypeTables();
}

bool PrescanAndSemaDebugAction::beginSourceFileAction() {
Expand All @@ -137,8 +137,8 @@ bool PrescanAndSemaDebugAction::beginSourceFileAction() {
// from exiting early (i.e. in the presence of semantic errors). We should
// never do this in actions intended for end-users or otherwise regular
// compiler workflows!
return runPrescan() && runParse() && (runSemanticChecks() || true) &&
(generateRtTypeTables() || true);
return runPrescan() && runParse(/*emitMessages=*/false) &&
(runSemanticChecks() || true) && (generateRtTypeTables() || true);
}

static void addDependentLibs(mlir::ModuleOp &mlirModule, CompilerInstance &ci) {
Expand Down Expand Up @@ -275,8 +275,8 @@ bool CodeGenAction::beginSourceFileAction() {
ci.getDiagnostics().Report(diagID);
return false;
}
bool res = runPrescan() && runParse() && runSemanticChecks() &&
generateRtTypeTables();
bool res = runPrescan() && runParse(/*emitMessages=*/false) &&
runSemanticChecks() && generateRtTypeTables();
if (!res)
return res;

Expand Down
5 changes: 4 additions & 1 deletion flang/lib/Semantics/semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ bool Semantics::Perform() {
ModFileWriter{context_}.WriteAll();
}

void Semantics::EmitMessages(llvm::raw_ostream &os) const {
void Semantics::EmitMessages(llvm::raw_ostream &os) {
// Resolve the CharBlock locations of the Messages to ProvenanceRanges
// so messages from parsing and semantics are intermixed in source order.
context_.messages().ResolveProvenances(context_.allCookedSources());
context_.messages().Emit(os, context_.allCookedSources());
}

Expand Down
7 changes: 7 additions & 0 deletions flang/test/Driver/message-merging.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
!RUN: %flang -fsyntax-only -pedantic -I %S/Inputs/ %s 2>&1 | FileCheck %s
!CHECK: warning: SAVE attribute was already specified on 'x'
!CHECK: portability: #include: extra stuff ignored after file name
save x
save x
#include <empty.h> crud after header name
end
11 changes: 7 additions & 4 deletions flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,20 @@ static mlir::LogicalResult convertFortranSourceToMLIR(

// parse the input Fortran
parsing.Parse(llvm::outs());
parsing.messages().Emit(llvm::errs(), parsing.allCooked());
if (!parsing.consumedWholeFile()) {
parsing.messages().Emit(llvm::errs(), parsing.allCooked());
parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
"parser FAIL (final position)",
"error: ", llvm::raw_ostream::RED);
return mlir::failure();
}
if ((!parsing.messages().empty() && (parsing.messages().AnyFatalError())) ||
!parsing.parseTree().has_value()) {
} else if ((!parsing.messages().empty() &&
(parsing.messages().AnyFatalError())) ||
!parsing.parseTree().has_value()) {
parsing.messages().Emit(llvm::errs(), parsing.allCooked());
llvm::errs() << programPrefix << "could not parse " << path << '\n';
return mlir::failure();
} else {
semanticsContext.messages().Annex(std::move(parsing.messages()));
}

// run semantics
Expand Down