Skip to content
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

"In file included from" diagnostic prelude omitted incorrectly if preceding note from the same file #62001

Open
smeenai opened this issue Apr 7, 2023 · 4 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@smeenai
Copy link
Collaborator

smeenai commented Apr 7, 2023

a.cpp:

#include "b.h"
#include "c.h"

b.h:

void b1();
template <class T> void b2(T x, T y) { x + y; }

c.h:

void c() {
    b1(0);
    b2("0", "0");
}

Run the following:

$ clang -fsyntax-only a.cpp
In file included from a.cpp:2:
./c.h:2:5: error: no matching function for call to 'b1'
    b1(0);
    ^~
./b.h:1:6: note: candidate function not viable: requires 0 arguments, but 1 was provided
void b1();
     ^
./b.h:2:42: error: invalid operands to binary expression ('const char *' and 'const char *')
template <class T> void b2(T x, T y) { x + y; }
                                       ~ ^ ~
./c.h:3:5: note: in instantiation of function template specialization 'b2<const char *>' requested here
    b2("0", "0");
    ^
2 errors generated.

Note that the ./b.h:2:42: error: line is missing the In file included from prelude indicating the source of the include. This seems to occur when the previously generated note was from the same file the error is being generated from; if I change the b1(0) line to b3() instead, so that the first error doesn't have a note from b.h, then the second error does get the expected prelude.

I discovered this while debugging a script to parse Clang error output. I imagine that SARIF output will be a better option for that sort of work once it's stabilized, but it doesn't currently seem to include the "In file included from" information, which is useful for my purposes.

@smeenai smeenai added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Apr 7, 2023
@shafik
Copy link
Collaborator

shafik commented Apr 8, 2023

@AaronBallman do you have a feeling for why the diagnostic is failing in this way?

@AaronBallman
Copy link
Collaborator

I believe this behavior is on purpose to reduce the amount of diagnostic noise -- we silence include stacks from notes by default, but we have -fdiagnostics-show-note-include-stack as an option to add this extra information. With that, I get:

F:\source\llvm-project>llvm\out\build\x64-Debug\bin\clang.exe -fsyntax-only -fdiagnostics-show-note-include-stack "C:\Users\aballman\OneDrive - Intel Corporation\Desktop\a.cpp"
In file included from C:\Users\aballman\OneDrive - Intel Corporation\Desktop\a.cpp:2:
C:\Users\aballman\OneDrive - Intel Corporation\Desktop/c.h:2:5: error: no matching function for call to 'b1'
    b1(0);
    ^~
In file included from C:\Users\aballman\OneDrive - Intel Corporation\Desktop\a.cpp:1:
C:\Users\aballman\OneDrive - Intel Corporation\Desktop/b.h:1:6: note: candidate function not viable: requires 0
      arguments, but 1 was provided
void b1();
     ^
C:\Users\aballman\OneDrive - Intel Corporation\Desktop/b.h:2:42: error: invalid operands to binary expression
      ('const char *' and 'const char *')
template <class T> void b2(T x, T y) { x + y; }
                                       ~ ^ ~
In file included from C:\Users\aballman\OneDrive - Intel Corporation\Desktop\a.cpp:2:
C:\Users\aballman\OneDrive - Intel Corporation\Desktop/c.h:3:5: note: in instantiation of function template
      specialization 'b2<const char *>' requested here
    b2("0", "0");
    ^
2 errors generated.

@smeenai
Copy link
Collaborator Author

smeenai commented Apr 10, 2023

Thanks, I wasn't aware of that option. In this case though, I want the include stack for the line

C:\Users\aballman\OneDrive - Intel Corporation\Desktop/b.h:2:42: error:

My understanding is that include stacks for errors were only supposed to be suppressed if the prior error was from the same file, but in this case we're suppressing when the prior note was from the same file. (It might still make sense to do that with -fdiagnostics-show-note-include-stack, but without that, we're completely missing the include stack for the error.)

@AaronBallman
Copy link
Collaborator

Oh, thank you for the clarification, you're right, the preface is missing from the second error still (my eyes glossed over that). I stepped through in a debugger and we're hitting this early return: https://github.com/llvm/llvm-project/blob/main/clang/lib/Frontend/DiagnosticRenderer.cpp#L171 -- we're storing the last location from the note and comparing that against the location for the subsequent error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

3 participants