149 Commits

Author SHA1 Message Date
Timm Baeder
718aac9f7a
[clang][Diagnostics] Highlight code snippets (#66514)
Add some primitive syntax highlighting to our code snippet output.

This adds "checkpoints" to the Preprocessor, which we can use to start lexing from. When printing a code snippet, we lex from the nearest checkpoint and highlight the tokens based on their token type.
2024-01-27 17:52:20 +01:00
Timm Baeder
918863deb0
[clang][Diagnostics] Make 'note' color CYAN (#66997)
Just using BLACK makes it invisible in terminals with a dark background.
2023-09-27 14:07:10 +02:00
Jan Svoboda
5a3130e3b6 [clang] NFCI: Use FileEntryRef in FileManager::getCanonicalName() 2023-09-13 15:31:58 -07:00
Jan Svoboda
523c471250 Reapply "[clang] NFCI: Adopt SourceManager::getFileEntryRefForID()"
This reapplies ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f, except for a tiny part that was reverted separately: 65331da0032ab4253a4bc0ddcb2da67664bd86a9. That will be reapplied later on, since it turned out to be more involved.

This commit is enabled by 5523fefb01c282c4cbcaf6314a9aaf658c6c145f and f0f548a65a215c450d956dbcedb03656449705b9, specifically the part that makes 'clang-tidy/checkers/misc/header-include-cycle.cpp' separator agnostic.
2023-09-08 19:04:01 -07:00
Jan Svoboda
0a9611fd8d Revert "[clang] NFCI: Adopt SourceManager::getFileEntryRefForID()"
This reverts commit ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f.

The 'clang-tidy/checkers/misc/header-include-cycle.cpp' test started failing on Windows: https://lab.llvm.org/buildbot/#/builders/216/builds/26855.
2023-09-06 13:23:23 -07:00
Jan Svoboda
ddbcc10b9e [clang] NFCI: Adopt SourceManager::getFileEntryRefForID()
This commit replaces some calls to the deprecated `FileEntry::getName()` with `FileEntryRef::getName()` by swapping current usages of `SourceManager::getFileEntryForID()` with `SourceManager::getFileEntryRefForID()`. This lowers the number of usages of the deprecated `FileEntry::getName()` from 95 to 50.
2023-09-06 10:49:48 -07:00
Takuya Shimizu
ef5217b3c0
[clang][Diagnostics] Fix wrong line number display (#65238)
When the caret location is lower than the lowest source range, clang is
printing wrong line numbers. The first line number should consider caret
location line even when there are source ranges provided.

Current wrong line example: https://godbolt.org/z/aj4qEjzs4
2023-09-05 12:12:42 +09:00
Timm Bäder
8554a55d04 [clang][Diagnostics] Fix diagnostic line numbers
The first line of the code snippet we print is potentially lower than
the caret line, so handle that case.

Fixes #63524

Differential Revision: https://reviews.llvm.org/D153849
2023-06-30 10:57:10 +02:00
Timm Bäder
6669dc09a4 [clang][NFC] Refactor printableTextForNextCharacter
Differential Revision: https://reviews.llvm.org/D150843
2023-06-10 07:51:36 +02:00
Charalampos Mitrodimas
91be60b347 Respect "-fdiagnostics-absolute-paths" on emit include location
This commit fixes "TextDiagnostic::emitIncludeLocation" when compiling
with "-fdiagnostics-absolute-paths" flag enabled by emitting the absolute
path of the included file.

Fixes #63026

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D151833
2023-06-05 18:07:18 +00:00
Timm Bäder
997c2f70c1 [clang][Diagnostics] Split source ranges into line ranges before...
... emitting them.

This makes later code easier to understand, since we emit the code
snippets line by line anyway.
It also fixes the weird underlinig of multi-line source ranges.

Differential Revision: https://reviews.llvm.org/D151215
2023-06-05 14:56:58 +02:00
Timm Bäder
808004f82a [clang][NFC] Reformat expandTabs
Use the proper capitalization here as well as a init capture for better
identifiers.
2023-06-04 07:52:46 +02:00
Timm Bäder
a0d83c3dc3 Revert "[clang][Diagnostics] Split source ranges into line ranges before..."
This reverts commit fc1262bd58ac54ad0a0bfa9750254b81c742bbb5.

This causes build bot failures because of a parser test case:
https://lab.llvm.org/buildbot/#/builders/139/builds/41961
2023-06-02 09:02:34 +02:00
Timm Bäder
fc1262bd58 [clang][Diagnostics] Split source ranges into line ranges before...
... emitting them.

This makes later code easier to understand, since we emit the code
snippets line by line anyway.
It also fixes the weird underlinig of multi-line source ranges.

Differential Revision: https://reviews.llvm.org/D151215
2023-06-02 08:52:03 +02:00
Timm Bäder
895b555378 [clang][Diagnostics][NFC] Move variable decl into if statement 2023-05-31 10:49:06 +02:00
Timm Bäder
b5c7892d54 [clang][Diagnostics][NFC] Move Buf{Start,End} out of the loop
They don't change inside the loop.
2023-05-31 10:21:24 +02:00
Timm Bäder
a806b3f496 [clang][Diagnostics][NFC] Remove unnecessary StringRef
Seems unnecessary to create a StringRef here just so we can drop the
trailing null bytes. We can do that with the std::string we
create anyway.

Differential Revision: https://reviews.llvm.org/D151300
2023-05-31 10:21:24 +02:00
Timm Bäder
3184fb9580 [clang][Diagnostics] Print empty lines in multiline snippets
We should preserve empty lines in output snippets.

Differential Revision: https://reviews.llvm.org/D151301
2023-05-31 10:21:24 +02:00
Timm Bäder
01578153ee [clang][Diagnostics][NFC] Don't create oversized CaretLine
Instead of creating a CaretLine the size of the SourceLine, just leave
it empty at first, let HighlightRange resize it to fit all the ~, then
resize it to fit the ^. Then we can save ourselves the work to remove
the trailing whitespace again.

Differential Revision: https://reviews.llvm.org/D151286
2023-05-31 10:21:24 +02:00
Timm Bäder
543c92969b [clang][Diagnostics][NFC] Remove unused Indentation parameter
printWordWrapped() is only called in one place, which passes all
parameters except `Indentation`. So, remove that parameter and use its
default value instead. Also remove the other default parameter values,
since those are unneeded.
2023-05-31 10:17:03 +02:00
Timm Bäder
50f0b8d219 [clang][Diagnostics][NFC] Merge byte/column mapping functions into one.
They were both only called from one place and did very similar things.
Merge them into one, so we only have to iterate the source line once to
generate the SourceMap.

Differential Revision: https://reviews.llvm.org/D151100
2023-05-31 10:00:08 +02:00
Timm Bäder
bb6c036ef3 [clang][Diagnostics][NFC] Use getFileID() directly
Instead of calling getDecomposedLoc() and then only using the FileID.
2023-05-31 09:57:16 +02:00
Timm Bäder
c113cbb510 [clang][Diagnostic][NFC] Simplify emitDiagnosticLoc
We don't use the offset returned from SourceManager::getDecomposedLoc
here, so we might as well just use getFileID().

Differential Revision: https://reviews.llvm.org/D151093
2023-05-31 09:40:24 +02:00
Timm Bäder
1c765483fd [clang][Diagnostics] Use llvm::raw_ostream::indent()
Differential Revision: https://reviews.llvm.org/D151078
2023-05-31 09:40:24 +02:00
Timm Bäder
49843c5036 [clang][Diagnostics] Simplify emitSnippet()
Don't try to minimize the times we invoke operator<< on the output
stream by keeping a ToPrint string around. Instead, just print the
characters as we iterate over them.

Differential Revision: https://reviews.llvm.org/D151075
2023-05-31 09:40:24 +02:00
Timm Bäder
b7e5cb1f9a [clang][NFC] Refactor emitSnippet()
Rename parameters and local variables and reorder things a bit to be
closer to their first point of use.

Differential Revision: https://reviews.llvm.org/D150840
2023-05-31 09:21:23 +02:00
Timm Bäder
f63155aaa6 [clang] Show line numbers in diagnostic code snippets
Show line numbers to the left of diagnostic code snippets and increase
the numbers of lines shown from 1 to 16.

Differential Revision: https://reviews.llvm.org/D147875
2023-05-31 07:26:03 +02:00
Timm Bäder
5fd35c326a Revert "[clang] Show line numbers in diagnostic code snippets"
This reverts commit e2917311f026cc445fa8aeefa0457b0c7a60824a.

This caused some problems with lldb testing the diagnostic output:
https://lab.llvm.org/buildbot/#/builders/68/builds/52754
2023-05-16 17:26:48 +02:00
Timm Bäder
e2917311f0 [clang] Show line numbers in diagnostic code snippets
Show line numbers to the left of diagnostic code snippets and increase
the numbers of lines shown from 1 to 16.

Differential Revision: https://reviews.llvm.org/D147875
2023-05-16 17:12:47 +02:00
Timm Bäder
dfafb7fe5a [clang][NFC] More range for loops in TextDiagnostic.cpp 2023-04-13 13:43:57 +02:00
Timm Bäder
650d69ffe4 [clang][NFC] Fix comment typo 2023-04-13 13:42:58 +02:00
Timm Bäder
37047523a9 [clang][NFC] Use range-for loop in TextDiagnostic.cpp 2023-04-12 18:50:37 +02:00
Kazu Hirata
ebd3eef0b2 [Frontend] Use std::optional in TextDiagnostic.cpp (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09 23:22:41 -08:00
Kazu Hirata
5891420e68 [clang] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:46 -08:00
Kazu Hirata
f5a68feab3 Use llvm::none_of (NFC) 2022-08-14 16:25:39 -07:00
Abraham Corea Diaz
119d22310b [clang] Add -fdiagnostics-format=sarif option for future SARIF output
Adds `sarif` option to the existing `-fdiagnostics-format` flag
for intended future work with SARIF diagnostics. Currently issues a warning
against the use of diagnostics in SARIF mode, then defaults to clang style for
diagnostics.

Reviewed By: cjdb, denik, aaron.ballman

Differential Revision: https://reviews.llvm.org/D129886
2022-07-21 16:51:15 +00:00
Sam McCall
b2a7f1c390 Remove a few effectively-unused FileEntry APIs. NFC
- isValid: FileManager only ever returns valid FileEntries (see next point)

- construction from outside FileManager (both FileEntry and DirectoryEntry).
  It's not possible to create a useful FileEntry this way, there are no setters.
  This was only used in FileEntryTest, added a friend to enable this.
  A real constructor is cleaner but requires larger changes to FileManager.

Differential Revision: https://reviews.llvm.org/D123197
2022-04-07 16:45:47 +02:00
Kazu Hirata
40446663c7 [clang] Use true/false instead of 1/0 (NFC)
Identified with modernize-use-bool-literals.
2022-01-09 00:19:47 -08:00
Hans Wennborg
6625680a58 [clang-cl] Remove the /fallback option
As discussed in
https://lists.llvm.org/pipermail/cfe-dev/2021-January/067524.html

It doesn't appear to be used, isn't really maintained, and adds some
complexity to the code. Let's remove it.

Differential revision: https://reviews.llvm.org/D95876
2021-02-04 10:33:16 +01:00
Raphael Isemann
659f4bd87e [clang] Add an option for hiding line numbers in diagnostics
Clang offers a `-f[no]-show-column` flag for hiding the column numbers when
printing diagnostics but there is no option for doing the same with line
numbers.

In LLDB having this option would be useful, as LLDB sometimes only knows the
file name for a SourceLocation and just assigns it the dummy line/column `1:1`.
These fake line/column numbers are confusing to the user and LLDB should be able
to tell clang to hide *both* the column and the line number when rendering text
diagnostics.

This patch adds a flag for also hiding the line numbers. It's not exposed via
the command line flags as it's most likely not very useful for any user and can
lead to ambiguous output when the user decides to only hide either the line or
the column number (where `file:1: ...` could now refer to both line 1 or column
1 depending on the compiler flags). LLDB can just access the DiagnosticOptions
directly when constructing its internal Clang instance.

The effect doesn't apply to Vi/MSVC style diagnostics because it's not defined
how these diagnostic styles would show an omitted line number (MSVC doesn't have
such an option and Vi's line mode is theory only supporting line numbers if I
understand it correctly).

Reviewed By: thakis, MaskRay

Differential Revision: https://reviews.llvm.org/D83038
2020-11-05 16:10:18 +01:00
Karl-Johan Karlsson
e8efac4b15 [clang] Fix the canonicalization of paths in -fdiagnostics-absolute-paths
In the current implementation of clang the canonicalization of paths in
diagnostic messages (when using -fdiagnostics-absolute-paths) only works
if the symbolic link is in the directory part of the filename, not if
the file itself is a symbolic link to another file.

This patch adds support to canonicalize the complete path including the
file.

Reviewers: rsmith, hans, rnk, ikudrin

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D70527
2019-12-20 09:16:33 +01:00
Raphael Isemann
1442efea9a [lldb] Print better diagnostics for user expressions and modules
Summary:
Currently our expression evaluators only prints very basic errors that are not very useful when writing complex expressions.

For example, in the expression below the user made a type error, but it's not clear from the diagnostic what went wrong:
```
(lldb) expr printf("Modulos are:", foobar%mo1, foobar%mo2, foobar%mo3)
error: invalid operands to binary expression ('int' and 'double')
```

This patch enables full Clang diagnostics in our expression evaluator. After this patch the diagnostics for the expression look like this:

```
(lldb) expr printf("Modulos are:", foobar%mo1, foobar%mo2, foobar%mo3)
error: <user expression 1>:1:54: invalid operands to binary expression ('int' and 'float')
printf("Modulos are:", foobar%mo1, foobar%mo2, foobar%mo3)
                                               ~~~~~~^~~~
```

To make this possible, we now emulate a user expression file within our diagnostics. This prevents that the user is exposed to
our internal wrapper code we inject.

Note that the diagnostics that refer to declarations from the debug information (e.g. 'note' diagnostics pointing to a called function)
will not be improved by this as they don't have any source locations associated with them, so caret or line printing isn't possible.
We instead just suppress these diagnostics as we already do with warnings as they would otherwise just be a context message
without any context (and the original diagnostic in the user expression should be enough to explain the issue).

Fixes rdar://24306342

Reviewers: JDevlieghere, aprantl, shafik, #lldb

Reviewed By: JDevlieghere, #lldb

Subscribers: usaxena95, davide, jingham, aprantl, arphaman, kadircet, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D65646

llvm-svn: 372203
2019-09-18 08:53:35 +00:00
Rui Ueyama
4d41c332ef Revert r367649: Improve raw_ostream so that you can "write" colors using operator<<
This reverts commit r367649 in an attempt to unbreak Windows bots.

llvm-svn: 367658
2019-08-02 07:22:34 +00:00
Rui Ueyama
a52f982f1c Improve raw_ostream so that you can "write" colors using operator<<
1. raw_ostream supports ANSI colors so that you can write messages to
the termina with colors. Previously, in order to change and reset
color, you had to call `changeColor` and `resetColor` functions,
respectively.

So, if you print out "error: " in red, for example, you had to do
something like this:

  OS.changeColor(raw_ostream::RED);
  OS << "error: ";
  OS.resetColor();

With this patch, you can write the same code as follows:

  OS << raw_ostream::RED << "error: " << raw_ostream::RESET;

2. Add a boolean flag to raw_ostream so that you can disable colored
output. If you disable colors, changeColor, operator<<(Color),
resetColor and other color-related functions have no effect.

Most LLVM tools automatically prints out messages using colors, and
you can disable it by passing a flag such as `--disable-colors`.
This new flag makes it easy to write code that works that way.

Differential Revision: https://reviews.llvm.org/D65564

llvm-svn: 367649
2019-08-02 04:48:30 +00:00
Harlan Haskins
8d323d1506 [clang] Adopt new FileManager error-returning APIs
Update the callers of FileManager::getFile and FileManager::getDirectory to handle the new llvm::ErrorOr-returning methods.

Signed-off-by: Harlan Haskins <harlan@apple.com>
llvm-svn: 367616
2019-08-01 21:31:56 +00:00
Igor Kudrin
0de4e935bb Do not resolve directory junctions for -fdiagnostics-absolute-paths on Windows.
If the source file path contains directory junctions, and we resolve them when
printing diagnostic messages, these paths look independent for an IDE.
For example, both Visual Studio and Visual Studio Code open separate editors
for such paths, which is not only inconvenient but might even result in losing
changes made in one of them.

Differential Revision: https://reviews.llvm.org/D59415

llvm-svn: 361598
2019-05-24 04:46:22 +00:00
Fangrui Song
75e74e077c Range-style std::find{,_if} -> llvm::find{,_if}. NFC
llvm-svn: 357359
2019-03-31 08:48:19 +00:00
Harlan Haskins
06f64d53ae Replace clang::FileData with llvm::vfs::Status
Summary:
FileData was only ever used as a container for the values in
llvm::vfs::Status, so they might as well be consolidated.

The `InPCH` member was also always set to false, and unused.

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D58924

llvm-svn: 355368
2019-03-05 02:27:12 +00:00
Hans Wennborg
27a7885228 Remove extraneous space in MSVC-style diagnostic output
There was an extra space between the file location and the diagnostic
message:

  /tmp/a.c(1,12):  warning: unused parameter 'unused'

the tests didn't catch this due to FileCheck not running in --strict-whitespace mode.

Reported by Marco: http://lists.llvm.org/pipermail/cfe-dev/2019-February/061326.html

Differential revision: https://reviews.llvm.org/D58377

llvm-svn: 354351
2019-02-19 16:58:25 +00:00
Chandler Carruth
2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00