161 Commits

Author SHA1 Message Date
Shafik Yaghmour
2a66ce5edb
[Clang][NFC] Clarify some SourceManager related code (#153527)
Static analysis flagged the columns - 1 code, it was correct but the
assumption was not obvious. I document the assumption w/ assertions.

While digging through related code I found getColumnNumber that looks
wrong at first inspection and adding parentheses makes it clearer.
2025-08-20 13:57:37 -07:00
Sirraide
7b43c6c6a7
Revert "[Clang] [Diagnostics] Simplify filenames that contain '..'" (#148367)
Revert llvm/llvm-project#143520 for now since it’s causing issues for
people who are using symlinks and prefer to preserve the original path
(i.e. looks like we’ll have to make this configurable after all; I just
need to figure out how to pass `-no-canonical-prefixes` down through the
driver); I’m planning to refactor this a bit and reland it in a few
days.
2025-07-12 15:13:22 +02:00
Sirraide
e3e7393c46
[Clang] [Diagnostics] Simplify filenames that contain '..' (#143520)
This can significantly shorten file paths to standard library headers,
e.g. on my system, `<ranges>` is currently printed as
```console
/usr/lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/ranges
```
but with this change, we instead print
```console
/usr/include/c++/15/ranges
```

This is of course just a heuristic; there are paths that would get longer
as a result of this, so we use whichever path ends up being shorter.

@AaronBallman pointed out that this might be problematic for network
file systems since path resolution might take a while, so this is enabled 
only for paths that are part of a local filesystem—though not on Windows
since there we noticed that the check itself is slow.

The file names are cached in `SourceManager`.
2025-07-08 01:02:19 +02:00
Haojian Wu
0b6ddb02ef
[clang] NFC: Add alias for std::pair<FileID, unsigned> used in SourceLocation (#145711)
Introduce a type alias for the commonly used `std::pair<FileID,
unsigned>` to improve code readability, and make it easier for future
updates (64-bit source locations).
2025-06-26 14:12:51 +02:00
Kazu Hirata
52e3b100d4
[Frontend] Remove unused includes (NFC) (#142256)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-31 15:03:52 -07:00
Jan Svoboda
13e1a2cb22 Reapply "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit e2a885537f11f8d9ced1c80c2c90069ab5adeb1d. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22 12:52:03 -07:00
Kazu Hirata
e2a885537f Revert "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c.

Multiple builtbot failures have been reported:
https://github.com/llvm/llvm-project/pull/139584
2025-05-22 12:44:20 -07:00
Jan Svoboda
9e306ad460
[clang] Remove intrusive reference count from DiagnosticOptions (#139584)
The `DiagnosticOptions` class is currently intrusively
reference-counted, which makes reasoning about its lifetime very
difficult in some cases. For example, `CompilerInvocation` owns the
`DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and
only exposes an accessor returning `DiagnosticOptions &`. One would
think this gives `CompilerInvocation` exclusive ownership of the object,
but that's not the case:

```c++
void shareOwnership(CompilerInvocation &CI) {
  llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions();
  // ...
}
```

This is a perfectly valid pattern that is being actually used in the
codebase.

I would like to ensure the ownership of `DiagnosticOptions` by
`CompilerInvocation` is guaranteed to be exclusive. This can be
leveraged for a copy-on-write optimization later on. This PR changes
usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and
`lldb` to not be intrusively reference-counted.
2025-05-22 12:33:52 -07:00
Alexander Kornienko
374fffe015
Fix out-of-bounds access to std::unique_ptr<T[]> (#111581)
This manifested as an assertion failure in Clang built against libc++
with
hardening enabled (e.g.
-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG):
`libcxx/include/__memory/unique_ptr.h:596: assertion
__checker_.__in_bounds(std::__to_address(__ptr_), __i) failed:
unique_ptr<T[]>::operator[](index): index out of range`
2024-10-09 14:15:06 +02:00
Fangrui Song
f5b93ae588 [clang] Fix -Wsign-compare in 32-bit builds 2024-06-22 13:40:05 -07:00
alexfh
a986f5e218
Fix clang crash when printing highlighted code in diagnostic (after #66514) (#80442)
Implements the fix proposed by Evgeny Eltsin on
https://github.com/llvm/llvm-project/pull/66514#issuecomment-1924039038.

No test case provided, since the bug is extremely sensitive to the
preprocessor
state (headers, macros, including the ones defined on command line), and
it
turned out to be non-trivial to create an isolated test.
2024-02-02 16:31:45 +01:00
Timm Baeder
6d1d2c67e7
[clang] Fix a possible out-of-bounds read (#80023)
Fixes #79964
2024-02-02 11:15:57 +01:00
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