aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorZiqing Luo <ziqing@udel.edu>2024-06-13 22:44:24 -0700
committerGitHub <noreply@github.com>2024-06-13 22:44:24 -0700
commit2e7b95e4c080426e5085c38cec01176b56798534 (patch)
treea5c2454c94a63a190830bb6278c26579e2890e77 /clang/lib/Basic/SourceManager.cpp
parent53dbc1f9f142c635e34b7fed3018f1954d0b573a (diff)
downloadllvm-2e7b95e4c080426e5085c38cec01176b56798534.zip
llvm-2e7b95e4c080426e5085c38cec01176b56798534.tar.gz
llvm-2e7b95e4c080426e5085c38cec01176b56798534.tar.bz2
[Safe Buffers] Serialize unsafe_buffer_usage pragmas (#92031)
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
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 753601e..f0af1a3 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -1915,6 +1915,24 @@ SourceManager::getDecomposedIncludedLoc(FileID FID) const {
return DecompLoc;
}
+FileID SourceManager::getUniqueLoadedASTFileID(SourceLocation Loc) const {
+ assert(isLoadedSourceLocation(Loc) &&
+ "Must be a source location in a loaded PCH/Module file");
+
+ auto [FID, Ignore] = getDecomposedLoc(Loc);
+ // `LoadedSLocEntryAllocBegin` stores the sorted lowest FID of each loaded
+ // allocation. Later allocations have lower FileIDs. The call below is to find
+ // the lowest FID of a loaded allocation from any FID in the same allocation.
+ // The lowest FID is used to identify a loaded allocation.
+ const FileID *FirstFID =
+ llvm::lower_bound(LoadedSLocEntryAllocBegin, FID, std::greater<FileID>{});
+
+ assert(FirstFID &&
+ "The failure to find the first FileID of a "
+ "loaded AST from a loaded source location was unexpected.");
+ return *FirstFID;
+}
+
bool SourceManager::isInTheSameTranslationUnitImpl(
const std::pair<FileID, unsigned> &LOffs,
const std::pair<FileID, unsigned> &ROffs) const {