aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/TextDiagnostic.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-20[Clang][NFC] Clarify some SourceManager related code (#153527)Shafik Yaghmour1-0/+2
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-07-12Revert "[Clang] [Diagnostics] Simplify filenames that contain '..'" (#148367)Sirraide1-1/+33
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-08[Clang] [Diagnostics] Simplify filenames that contain '..' (#143520)Sirraide1-33/+1
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-06-26[clang] NFC: Add alias for std::pair<FileID, unsigned> used in ↵Haojian Wu1-3/+3
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-05-31[Frontend] Remove unused includes (NFC) (#142256)Kazu Hirata1-2/+0
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-22Reapply "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Jan Svoboda1-40/+39
(#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-22Revert "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Kazu Hirata1-39/+40
(#139584)" This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c. Multiple builtbot failures have been reported: https://github.com/llvm/llvm-project/pull/139584
2025-05-22[clang] Remove intrusive reference count from `DiagnosticOptions` (#139584)Jan Svoboda1-40/+39
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.
2024-10-09Fix out-of-bounds access to std::unique_ptr<T[]> (#111581)Alexander Kornienko1-3/+3
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-06-22[clang] Fix -Wsign-compare in 32-bit buildsFangrui Song1-1/+1
2024-02-02Fix clang crash when printing highlighted code in diagnostic (after #66514) ↵alexfh1-1/+1
(#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[clang] Fix a possible out-of-bounds read (#80023)Timm Baeder1-1/+1
Fixes #79964
2024-01-27[clang][Diagnostics] Highlight code snippets (#66514)Timm Baeder1-9/+202
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.
2023-09-27[clang][Diagnostics] Make 'note' color CYAN (#66997)Timm Baeder1-2/+1
Just using BLACK makes it invisible in terminals with a dark background.
2023-09-13[clang] NFCI: Use `FileEntryRef` in `FileManager::getCanonicalName()`Jan Svoboda1-2/+2
2023-09-08Reapply "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda1-1/+1
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-06Revert "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda1-1/+1
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[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`Jan Svoboda1-1/+1
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-05[clang][Diagnostics] Fix wrong line number display (#65238)Takuya Shimizu1-2/+1
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-06-30[clang][Diagnostics] Fix diagnostic line numbersTimm Bäder1-1/+5
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-10[clang][NFC] Refactor printableTextForNextCharacterTimm Bäder1-51/+56
Differential Revision: https://reviews.llvm.org/D150843
2023-06-05Respect "-fdiagnostics-absolute-paths" on emit include locationCharalampos Mitrodimas1-4/+5
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[clang][Diagnostics] Split source ranges into line ranges before...Timm Bäder1-77/+89
... 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-04[clang][NFC] Reformat expandTabsTimm Bäder1-8/+8
Use the proper capitalization here as well as a init capture for better identifiers.
2023-06-02Revert "[clang][Diagnostics] Split source ranges into line ranges before..."Timm Bäder1-89/+77
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[clang][Diagnostics] Split source ranges into line ranges before...Timm Bäder1-77/+89
... 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-05-31[clang][Diagnostics][NFC] Move variable decl into if statementTimm Bäder1-2/+1
2023-05-31[clang][Diagnostics][NFC] Move Buf{Start,End} out of the loopTimm Bäder1-3/+2
They don't change inside the loop.
2023-05-31[clang][Diagnostics][NFC] Remove unnecessary StringRefTimm Bäder1-7/+5
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[clang][Diagnostics] Print empty lines in multiline snippetsTimm Bäder1-4/+1
We should preserve empty lines in output snippets. Differential Revision: https://reviews.llvm.org/D151301
2023-05-31[clang][Diagnostics][NFC] Don't create oversized CaretLineTimm Bäder1-12/+4
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[clang][Diagnostics][NFC] Remove unused Indentation parameterTimm Bäder1-9/+4
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[clang][Diagnostics][NFC] Merge byte/column mapping functions into one.Timm Bäder1-49/+36
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[clang][Diagnostics][NFC] Use getFileID() directlyTimm Bäder1-3/+1
Instead of calling getDecomposedLoc() and then only using the FileID.
2023-05-31[clang][Diagnostic][NFC] Simplify emitDiagnosticLocTimm Bäder1-9/+5
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[clang][Diagnostics] Use llvm::raw_ostream::indent()Timm Bäder1-15/+5
Differential Revision: https://reviews.llvm.org/D151078
2023-05-31[clang][Diagnostics] Simplify emitSnippet()Timm Bäder1-15/+12
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[clang][NFC] Refactor emitSnippet()Timm Bäder1-20/+18
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[clang] Show line numbers in diagnostic code snippetsTimm Bäder1-6/+44
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-16Revert "[clang] Show line numbers in diagnostic code snippets"Timm Bäder1-44/+6
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[clang] Show line numbers in diagnostic code snippetsTimm Bäder1-6/+44
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-04-13[clang][NFC] More range for loops in TextDiagnostic.cppTimm Bäder1-64/+60
2023-04-13[clang][NFC] Fix comment typoTimm Bäder1-1/+1
2023-04-12[clang][NFC] Use range-for loop in TextDiagnostic.cppTimm Bäder1-8/+5
2022-12-09[Frontend] Use std::optional in TextDiagnostic.cpp (NFC)Kazu Hirata1-1/+2
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[clang] Use std::nullopt instead of None (NFC)Kazu Hirata1-2/+3
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-08-14Use llvm::none_of (NFC)Kazu Hirata1-2/+1
2022-07-21[clang] Add -fdiagnostics-format=sarif option for future SARIF outputAbraham Corea Diaz1-0/+2
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-04-07Remove a few effectively-unused FileEntry APIs. NFCSam McCall1-2/+1
- 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-01-09[clang] Use true/false instead of 1/0 (NFC)Kazu Hirata1-1/+1
Identified with modernize-use-bool-literals.