This is an archive of the discontinued LLVM Phabricator instance.

[flang] Support the color diagnostics on scanning, parsing, and semantics
ClosedPublic

Authored by peixin on May 22 2022, 4:42 AM.

Details

Summary

The options -f{no-}color-diagnostics have been supported in driver. This
supports the behaviors in scanning, parsing, and semantics, and the
behaviors are exactly the same as the driver.

To illustrate the added behaviour, consider the following input file:

! file.f90
program m
  integer :: i = k
end

In the following invocations, "error: Must be a constant value" _will be_
formatted:

$ flang-new file.f90
error: Semantic errors in file.f90
./file.f90:2:18: error: Must be a constant value
    integer :: i = k

Note that "error: Semantic errors in file.f90" is also formatted, which
is supported in https://reviews.llvm.org/D126164.

Also note that only "error", "warning" and "portability" are formatted.
Check the following input file:

! file2.f90
program m
  integer :: i =
end
$ flang-new test2.f90
error: Could not parse test2.f90
./test2.f90:2:11: error: expected '('
    integer :: i =
            ^
./test2.f90:2:3: in the context: statement function definition
    integer :: i =
    ^
...

The "error: Could not parse test2.f90" and "error: expected '('" are
formatted. Others such as "in the context" are not formatted yet, which
may or may not be supported.

Diff Detail

Event Timeline

peixin created this revision.May 22 2022, 4:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2022, 4:42 AM
Herald added a subscriber: jdoerfert. · View Herald Transcript
peixin requested review of this revision.May 22 2022, 4:42 AM

Please check the following test cases after applying this patch. This highlight the color of the keywords error before "Must be a constant value" and portability.

$ cat case.f90 
program m
  integer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  integer :: i = k
  integer :: j = k
end
$ flang-new -fc1 case.f90 
error: Semantic errors in case.f90
./case.f90:2:11: portability: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx has length 72, which is greater than the maximum name length 63
    integer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
./case.f90:3:18: error: Must be a constant value
    integer :: i = k
                   ^
./case.f90:4:18: error: Must be a constant value
    integer :: j = k
                   ^

Hi @peixin , thanks for proposing this!

I think that Flang end users will really appreciate this. However (I already hinted this in https://reviews.llvm.org/D126164), we should be able to disable this formatting (with e.g. -fno-color-diagnostics) and have plain text messages in tests (i.e. default to -fno-color-diagnostics there). Let me know if you need any help with this!

Hi @peixin , thanks for proposing this!

I think that Flang end users will really appreciate this. However (I already hinted this in https://reviews.llvm.org/D126164), we should be able to disable this formatting (with e.g. -fno-color-diagnostics) and have plain text messages in tests (i.e. default to -fno-color-diagnostics there). Let me know if you need any help with this!

Thanks @awarzynski . Currently, I have no idea of how to add one option. Could you please help me provide the direction where I can follow?

Hi @peixin , thanks for proposing this!

I think that Flang end users will really appreciate this. However (I already hinted this in https://reviews.llvm.org/D126164), we should be able to disable this formatting (with e.g. -fno-color-diagnostics) and have plain text messages in tests (i.e. default to -fno-color-diagnostics there). Let me know if you need any help with this!

Thanks @awarzynski . Currently, I have no idea of how to add one option. Could you please help me provide the direction where I can follow?

Here are some relevant docs:

Some grasp of how things work in Clang is both helpful (Flang's driver has been inspired by Clang) and required (Flang and Clang share Options.td).

As for -f{no-}color-diagnostics specifically, there's parseShowColorsArgs that processes these options in the frontend driver (CompilerInvocation belongs to the frontend driver). In other words, these options are already enabled in Flang, but clearly some plumbing is missing. In the compiler driver, I see this in Clang.cpp, but nothing of this sort in Flang.cpp 🤔 .

Note that the above is mostly about parsing the option as provided by the user. Once an option is parsed, you will need to make sure that it is used to configure the frontend. You could use FrontendOptions in the driver. You will also have to tweak the frontend API (e.g. AllSources::EmitMessage) to make sure that the formatting is not unconditional.

Lastly, I would skip any changes "bbc.cpp". Whatever logic is added in flang-new, it won't work in bbc. So you can either leave bbc as is or you will have to add -f{no-}color-diagnostics there independently. Unconditional formatting of the diagnostics is not desirable IMHO.

Apologies if this feels like "waving hands" - happy to answer more questions!

peixin added a comment.Jun 1 2022, 6:38 AM

Hi @peixin , thanks for proposing this!

I think that Flang end users will really appreciate this. However (I already hinted this in https://reviews.llvm.org/D126164), we should be able to disable this formatting (with e.g. -fno-color-diagnostics) and have plain text messages in tests (i.e. default to -fno-color-diagnostics there). Let me know if you need any help with this!

Thanks @awarzynski . Currently, I have no idea of how to add one option. Could you please help me provide the direction where I can follow?

Here are some relevant docs:

Some grasp of how things work in Clang is both helpful (Flang's driver has been inspired by Clang) and required (Flang and Clang share Options.td).

As for -f{no-}color-diagnostics specifically, there's parseShowColorsArgs that processes these options in the frontend driver (CompilerInvocation belongs to the frontend driver). In other words, these options are already enabled in Flang, but clearly some plumbing is missing. In the compiler driver, I see this in Clang.cpp, but nothing of this sort in Flang.cpp 🤔 .

Note that the above is mostly about parsing the option as provided by the user. Once an option is parsed, you will need to make sure that it is used to configure the frontend. You could use FrontendOptions in the driver. You will also have to tweak the frontend API (e.g. AllSources::EmitMessage) to make sure that the formatting is not unconditional.

Lastly, I would skip any changes "bbc.cpp". Whatever logic is added in flang-new, it won't work in bbc. So you can either leave bbc as is or you will have to add -f{no-}color-diagnostics there independently. Unconditional formatting of the diagnostics is not desirable IMHO.

Apologies if this feels like "waving hands" - happy to answer more questions!

@awarzynski Thanks so much for there references. These are very helpful. I will look into these and will let know if I have further questions.

peixin planned changes to this revision.Jun 26 2022, 7:17 PM
peixin updated this revision to Diff 442256.Jul 5 2022, 4:07 AM
peixin retitled this revision from [flang] Set the color for the keyword of each single diag message to [flang] Support the color diagnostics on scanning, parsing, and semantics.
peixin edited the summary of this revision. (Show Details)

These changes nicely complement what you did in https://reviews.llvm.org/D126164 and will definitely improve the end-user experience. Thank you for working on this!

The implementation makes sense to me, but since I've not contributed any code in this area, I'd prefer to defer to somebody more familiar with the frontend. Perhaps @klausler or @jeanPerier ?

These changes nicely complement what you did in https://reviews.llvm.org/D126164 and will definitely improve the end-user experience. Thank you for working on this!

The implementation makes sense to me, but since I've not contributed any code in this area, I'd prefer to defer to somebody more familiar with the frontend. Perhaps @klausler or @jeanPerier ?

OK. Thanks.

awarzynski accepted this revision.Jul 25 2022, 9:54 AM

@peixin, I think that it would be great to have this available in LLVM Flang. Ideally, folks most active in this area would review it, but sometimes that's not possible. IMHO, this is rather straightforward and looks good to me, thanks for working on it!

I've left a few small suggestions, but nothing major. Feel free to ignore. Please way at least a day or two before merging in case people leave some extra comments!

flang/lib/Parser/provenance.cpp
232

[nit] I don't think that you need a switch statement here. Instead, I would write something like (pseudo code):

if (color && showColors) {
  o.changeColor(color, true);
  o << prefix;
  o.resetColor();
  return;
}

o << prefix;

or:

if (color && showColors)
  o.changeColor(color, true);

o << prefix;

if (color && showColors)
  o.resetColor();

(you may need to use std::optional for color for this to work).

flang/test/Driver/color-diagnostics-parse.f90
2
flang/test/Driver/color-diagnostics-scan.f
2
flang/test/Driver/color-diagnostics-sema.f90
2
This revision is now accepted and ready to land.Jul 25 2022, 9:54 AM
peixin updated this revision to Diff 447643.Jul 26 2022, 4:32 AM

Thanks @awarzynski for the review. Address the comments.

I will land this patch tomorrow if there is no further comments.