aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
AgeCommit message (Collapse)AuthorFilesLines
2024-09-20Revert "[SystemZ][z/OS] Propagate IsText parameter to open text files as ↵Abhina Sreeskantharajan1-12/+2
text (#107906)" This reverts commit edf3b277a5f2ebe144827ed47463c22743cac5f9.
2024-09-19[SystemZ][z/OS] Propagate IsText parameter to open text files as text (#107906)Abhina Sree1-2/+12
This patch adds an IsText parameter to the following functions openFileForRead, getBufferForFile, getBufferForFileImpl and determines whether a file is text by querying the file tag on z/OS. The default is set to OF_Text instead of OF_None, this change in value does not affect any other platforms other than z/OS.
2024-09-18Fix OOM in FormatDiagnostic (2nd attempt) (#108866)Vakhurin Sergei1-19/+4
Resolves: #70930 (and probably latest comments from clangd/clangd#251) by fixing racing for the shared DiagStorage value which caused messing with args inside the storage and then formatting the following message with getArgSInt(1) == 2: def err_module_odr_violation_function : Error< "%q0 has different definitions in different modules; " "%select{definition in module '%2'|defined here}1 " "first difference is " which causes HandleSelectModifier to go beyond the ArgumentLen so the recursive call to FormatDiagnostic was made with DiagStr > DiagEnd that leads to infinite while (DiagStr != DiagEnd). The Main Idea: Reuse the existing DiagStorageAllocator logic to make all DiagnosticBuilders having independent states. Also, encapsulating the rest of state (e.g. ID and Loc) into DiagnosticBuilder. The last attempt failed - https://github.com/llvm/llvm-project/pull/108187#issuecomment-2353122096 so was reverted - #108838
2024-09-16Revert "Fix OOM in FormatDiagnostic" (#108838)Aaron Ballman1-4/+19
Reverting due to build failures found in #108187
2024-09-16Fix OOM in FormatDiagnostic (#108187)Vakhurin Sergei1-19/+4
Resolves: #70930 (and probably latest comments from https://github.com/clangd/clangd/issues/251) by fixing racing for the shared `DiagStorage` value which caused messing with args inside the storage and then formatting the following message with `getArgSInt(1)` == 2: ``` def err_module_odr_violation_function : Error< "%q0 has different definitions in different modules; " "%select{definition in module '%2'|defined here}1 " "first difference is " ``` which causes `HandleSelectModifier` to go beyond the `ArgumentLen` so the recursive call to `FormatDiagnostic` was made with `DiagStr` > `DiagEnd` that leads to infinite `while (DiagStr != DiagEnd)`. **The Main Idea:** Reuse the existing `DiagStorageAllocator` logic to make all `DiagnosticBuilder`s having independent states. Also, encapsulating the rest of state (e.g. ID and Loc) into `DiagnosticBuilder`. **TODO (if it will be requested by reviewer):** - [x] add a test (I have no idea how to turn a whole bunch of my proprietary code which leads `clangd` to OOM into a small public example.. probably I must try using [this](https://github.com/llvm/llvm-project/issues/70930#issuecomment-2209872975) instead) - [x] [`Diag.CurDiagID != diag::fatal_too_many_errors`](https://github.com/llvm/llvm-project/pull/108187#pullrequestreview-2296395489) - [ ] ? get rid of `DiagStorageAllocator` at all and make `DiagnosticBuilder` having they own `DiagnosticStorage` coz it seems pretty small so should fit the stack for short-living `DiagnosticBuilder` instances
2024-08-30[clang] Cleanup IncludeLocMap (#106241)kadir çetinkaya1-0/+1
CompilerInstance can re-use same SourceManager across multiple frontendactions. During this process it calls `SourceManager::clearIDTables` to reset any caches based on FileIDs. It didn't reset IncludeLocMap, resulting in wrong include locations for workflows that triggered multiple frontend-actions through same CompilerInstance.
2024-08-16[libclang/python] Expose `clang_isBeforeInTranslationUnit` for ↵Jannick Kremer1-2/+1
`SourceRange.__contains__` Add libclang function `clang_isBeforeInTranslationUnit` to allow checking the order between two source locations. Simplify the `SourceRange.__contains__` implementation using this new function. Add tests for `SourceRange.__contains__` and the newly added functionality. Fixes #22617 Fixes #52827
2024-06-24[SourceManager] Expose max usage of source location space as a Statistic ↵Ilya Biryukov1-0/+17
(#96292) We have been running into source location exhaustion recently and want to use the statistics to monitor the usage in various files to be able to anticipate where the next problem will happen. I picked `Statistic` because it can be written into a structured JSON file and is easier to consume by further automation. This commit does not change any existing per-source-manager metrics exposed via `SourceManager::PrintStats()`. This does create some redundancy, but I also expect to be non-controversial because it aligns with the intended use of `Statistic`.
2024-06-13[Safe Buffers] Serialize unsafe_buffer_usage pragmas (#92031)Ziqing Luo1-0/+18
The commit adds serialization and de-serialization implementations for the stored regions. Basically, the serialized representation of the regions of a PP is a (ordered) sequence of source location encodings. For de-serialization, regions from loaded files are stored by their ASTs. When later one queries if a loaded location L is in an opt-out region, PP looks up the regions of the loaded AST where L is at. (Background if helps: a pair of `#pragma clang unsafe_buffer_usage begin/end` pragmas marks a warning-opt-out region. The begin and end locations (opt-out regions) are stored in preprocessor instances (PP) and will be queried by the `-Wunsafe-buffer-usage` analyzer.) The reported issue at upstream: https://github.com/llvm/llvm-project/issues/90501 rdar://124035402
2024-05-18[clang][NFC] Further improvements to const-correctnessVlad Serebrennikov1-3/+3
2024-05-17[clang][NFC] Improve const-correctness in `SourceManager` (#92436)Vlad Serebrennikov1-0/+4
This patch adds several const-qualified variants of existing member functions to `SourceManager`. I started with removing const qualification from `setNumCreatedFIDsForFileID`, and removing `const_cast` in the body of this function, as I think it doesn't make sense to const-qualify setters.
2023-11-06[clang] Improve `SourceManager::PrintStats()`Jan Svoboda1-5/+7
This fixes a typo ("SLocEntry's" -> "SLocEntries"), fixes capitalization ("Sloc" -> "SLoc") and adds extra information (capacity in bytes of `LoadedSLocEntryTable`).
2023-10-23[Clang] Report an error and crash on source location exhaustion in macros ↵Ilya Biryukov1-5/+11
(#69908) `createExpansionLocImpl` has an assert that checks if we ran out of source locations. We have observed this happening on real code and in release builds the assertion does not fire and the compiler just keeps running indefinitely without giving any indication that something went wrong. Diagnose this problem and reliably crash to make sure the problem is easy to detect. I have also tried: - returning invalid source locations, - reporting sloc address space usage on error. Both caused the compiler to run indefinitely. It would be nice to dig further why that happens, but until then crashing seems like a better alternative.
2023-10-10Use llvm::endianness::{big,little,native} (NFC)Kazu Hirata1-1/+1
Note that llvm::support::endianness has been renamed to llvm::endianness while becoming an enum class as opposed to an enum. This patch replaces llvm::support::{big,little,native} with llvm::endianness::{big,little,native}.
2023-10-06[clang][modules] Move `SLocEntry` search into `ASTReader` (#66966)Jan Svoboda1-65/+5
In `SourceManager::getFileID()`, Clang performs binary search over its buffer of `SLocEntries`. For modules, this binary search fully deserializes the entire `SLocEntry` block for each visited entry. For some entries, that includes decompressing the associated buffer (e.g. the predefines buffer, macro expansion buffers, contents of volatile files), which shows up in profiles of the dependency scanner. This patch moves the binary search over loaded entries into `ASTReader`, which can perform cheaper partial deserialization during the binary search, reducing the wall time of dependency scans by ~3%. This also reduces the number of retired instructions by ~1.4% on regular (implicit) modules compilation. Note that this patch drops the optimizations based on the last lookup ID (pruning the search space and performing linear search before resorting to the full binary search). Instead, it reduces the search space by asking `ASTReader::GlobalSLocOffsetMap` for the containing `ModuleFile` and only does binary search over entries of single module file.
2023-10-06[Basic] Fix a warningKazu Hirata1-1/+2
This patch fixes: clang/lib/Basic/SourceManager.cpp:1979:64: error: 'greater' may not intend to support class template argument deduction [-Werror,-Wctad-maybe-unsupported]
2023-10-06[clang][modules] Remove preloaded SLocEntries from PCM files (#66962)Jan Svoboda1-66/+99
This commit removes the list of SLocEntry offsets to preload eagerly from PCM files. Commit introducing this functionality (258ae54a) doesn't clarify why this would be more performant than the lazy approach used regularly. Currently, the only SLocEntry the reader is supposed to preload is the predefines buffer, but in my experience, it's not actually referenced in most modules, so the time spent deserializing its SLocEntry is wasted. This is especially noticeable in the dependency scanner, where this change brings 4.56% speedup on my benchmark.
2023-10-03[clang] NFCI: Use `FileEntryRef` for `FileID` creation (#67838)Jan Svoboda1-14/+4
This patch removes the `SourceManager` APIs that create `FileID` from a `const FileEntry *` in favor of APIs that take `FileEntryRef`. This also removes a misleading documentation that claims `nullptr` file entry represents stdin. I don't think that's right, since we just try to dereference that pointer anyways.
2023-09-30Introduce paged vector (#66430)Giulio Eulisse1-5/+5
The goal of the class is to be an (almost) drop in replacement for SmallVector and std::vector when those are presized and filled later, as it happens in SourceManager and ASTReader. By doing so, sparsely accessed PagedVector can profit from reduced memory footprint.
2023-09-29[clang] NFCI: Use `FileEntryRef` in ↵Jan Svoboda1-2/+2
`SourceManager::getMemoryBufferForFileOr{None,Fake}()`
2023-09-29[clang] NFCI: Use `FileEntryRef` in `SourceManager::overrideFileContents()`Jan Svoboda1-2/+2
2023-09-29[clang] NFCI: Use `FileEntryRef` in `SourceManager::FileInfos` (#67742)Jan Svoboda1-3/+2
2023-09-28[clang] NFCI: Use `FileEntryRef` in `SourceManager::setFileIsTransient()`Jan Svoboda1-2/+2
2023-09-28[clang][Parser][NFC] Fix a doc comment mishapTimm Bäder1-1/+1
2023-09-13[clang] NFCI: Use `FileEntryRef` in `SrcMgr::ContentCache`Jan Svoboda1-1/+1
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-05-24LLVM_FALLTHROUGH => [[fallthrough]]. NFCCraig Topper1-1/+1
Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D150996
2023-01-28Use llvm::count{lr}_{zero,one} (NFC)Kazu Hirata1-2/+1
2023-01-14[clang] Remove remaining uses of llvm::Optional (NFC)Kazu Hirata1-1/+0
This patch removes several "using" declarations and #include "llvm/ADT/Optional.h". 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
2023-01-14[clang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-12/+12
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to remove #include "llvm/ADT/Optional.h". 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
2023-01-14[clang] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with 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-20[Clang] Prepare for llvm::Optional becoming std::optional.Benjamin Kramer1-2/+2
The needed tweaks are mostly trivial, the one nasty bit is Clang's usage of OptionalStorage. To keep this working old Optional stays around as clang::CustomizableOptional, with the default Storage removed. Optional<File/DirectoryEntryRef> is replaced with a typedef. I tested this with GCC 7.5, the oldest supported GCC I had around. Differential Revision: https://reviews.llvm.org/D140332
2022-12-18Revert "[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to ↵Krzysztof Parzyszek1-3/+2
std::optional" This reverts commit 8f0df9f3bbc6d7f3d5cbfd955c5ee4404c53a75d. The Optional*RefDegradesTo*EntryPtr types want to keep the same size as the underlying type, which std::optional doesn't guarantee. For use with llvm::Optional, they define their own storage class, and there is no way to do that in std::optional. On top of that, that commit broke builds with older GCCs, where std::optional was not trivially copyable (static_assert in the clang sources was failing).
2022-12-17[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to std::optionalKrzysztof Parzyszek1-2/+3
2022-12-10Don't include None.h (NFC)Kazu Hirata1-1/+0
I've converted all known uses of None to std::nullopt, so we no longer need to include None.h. 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-04Correct typo introduced in f607884a04b0ca06951227a01d00bc59b948d337serge-sans-paille1-1/+1
Fix #59321
2022-12-03[Basic] Use std::nullopt instead of None (NFC)Kazu Hirata1-11/+11
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-11-30[Basic] Fix a warningKazu Hirata1-1/+1
This patch fixes: clang/lib/Basic/SourceManager.cpp:1292:19: error: comparison of integers of different signs: 'long' and 'unsigned long' [-Werror,-Wsign-compare]
2022-11-30[clang] Speedup LineOffsetMapping::getserge-sans-paille1-23/+25
LineOffsetMapping::get is a critical function that consistently appears in the top 5 more computation intensive functions when running the preprocessor. This change brings consistent speedup of ~.5% on, preprocessing time, see https://llvm-compile-time-tracker.com/compare.php?from=0745b0c0354a0c8e1fefb68a3876d15db6c2e27a&to=460f3f04dac025e6952d78fce104a88151508a29&stat=instructions:u for detailed statistics. Differential Revision: https://reviews.llvm.org/D138474
2022-11-16When we run out of source locations, try to produce useful informationRichard Smith1-0/+91
indicating why we ran out.
2022-11-08Fix duplicate word typos; NFCRageking81-1/+1
This revision fixes typos where there are 2 consecutive words which are duplicated. There should be no code changes in this revision (only changes to comments and docs). Do let me know if there are any undesirable changes in this revision. Thanks.
2022-10-18Fix incorrect check for running out of source locations.Paul Pluzhnikov1-1/+3
When CurrentLoadedOffset is less than TotalSize, current code will trigger unsigned overflow and will not return an "allocation failed" indicator. Google ref: b/248613299 Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D135192
2022-10-15[clang] Fix a warningKazu Hirata1-4/+5
This patch fixes: clang/lib/Basic/SourceManager.cpp:2131:72: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
2022-10-07[SourceManager] Fix the incorrect counting stats in getFileIDLoaded.Haojian Wu1-2/+0
We were double-counting the number of binary search FileID scans.
2022-10-07[SourceManager] Improve getFileIDLocal.Haojian Wu1-20/+17
Prune the search space -- If we know offset(LastFileIDLookup) < SearchOffset, we can prune the initial binary-search range from [0, end) to [LastFileIDlookup, end). It reduces the binary search scan by ~30%. SemaExpr.cpp: 1393437 -> 1035426 FindTarget.cpp: 1275930 -> 920087 Linux kernel: getFileIDLocal: 2.45% -> 2.15% Differential Revision: https://reviews.llvm.org/D135132
2022-10-06[SourceManager] Improve getFileIDLoaded.Haojian Wu1-16/+18
Similar to getFileIDLocal patch, but for the version for load module. Test with clangd (building AST with preamble), FileID scans in binary search is reduced: SemaExpr.cpp: 142K -> 137K (-3%) FindTarget.cpp: 368K -> 343K (-6%) Differential Revision: https://reviews.llvm.org/D135258
2022-10-05[clang] Update ModuleMap::getModuleMapFile* to use FileEntryRefBen Langmuir1-5/+8
Update SourceManager::ContentCache::OrigEntry to keep the original FileEntryRef, and use that to enable ModuleMap::getModuleMapFile* to return the original FileEntryRef. This change should be NFC for most users of SourceManager::ContentCache, but it could affect behaviour for users of getNameAsRequested such as in compileModuleImpl. I have not found a way to detect that difference without additional functional changes, other than incidental cases like changes from / to \ on Windows so there is no new test. Differential Revision: https://reviews.llvm.org/D135220
2022-10-05Fix SourceManager::isBeforeInTranslationUnit bug with token-pastingSam McCall1-28/+49
isBeforeInTranslationUnit compares SourceLocations across FileIDs by mapping them onto a common ancestor file, following include/expansion edges. It is possible to get a tie in the common ancestor, because multiple "chunks" of a macro arg will expand to the same macro param token in the body: #define ID(X) X #define TWO 2 ID(1 TWO) Here two FileIDs both expand into `X` in ID's expansion: - one containing `1` and spelled on line 3 - one containing `2` and spelled by the macro expansion of TWO isBeforeInTranslationUnit breaks this tie by comparing the two FileIDs: the one "on the left" is always created first and is numerically smaller. This seems correct so far. Prior to this patch it also takes a shortcut (unclear if intentionally). Instead of comparing the two FileIDs that directly expand to the same location, it compares the original FileIDs being compared. These may not be the same if there are multiple macro expansions in between. This *almost* always yields the right answer, because macro expansion yields "trees" of FileIDs allocated in a contiguous range: when comparing tree A to tree B, it doesn't matter what representative you pick. However, the splitting of >> tokens is modeled as macro expansion (as if the first '>' was a macro that expands to a '>' spelled a scratch buffer). This splitting occurs retroactively when parsing, so the FileID allocated is larger than expected if it were a real macro expansion performed during lexing. As a result, macro tree A can be on the left of tree B, and yet contain a token-split FileID whose numeric value is *greator* than those in B. In this case the tiebreak gives the wrong answer. Concretely: #define ID(X) X template <typename> class S{}; ID( ID(S<S<int>> x); int y; ) Given Greater = (typeloc of S<int>).getEndLoc(); Y = (decl of y).getLocation(); isBeforeInTranslationUnit(Greater, Y) should return true, but returns false. Here the common FileID of (Greater, Y) is the body of the outer ID expansion, and they both expand to X within it. With the current tiebreak rules, we compare the FileID of Greater (a split) to the FileID of Y (a macro arg expansion into X of the outer ID). The former is larger because the token split occurred relatively late. This patch fixes the issue by removing the shortcut. It tracks the immediate FileIDs used to reach the common file, and uses these IDs to break ties. In the example, we now compare the macro arg expansion of the inner ID() to the macro arg expansion of Y, and find that it is smaller. This requires some changes to the InBeforeInTUCacheEntry (sic). We store a little more data so it's probably slightly slower. It was difficult to resist more invasive changes: - performance: the sizing is very suspicious, and once the cache "fills up" we're thrashing a single entry - API: the class seems to be needlessly complicated However I tried to avoid mixing these with subtle behavior changes, and will send a followup instead. Differential Revision: https://reviews.llvm.org/D134685