aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2024-06-24 11:57:36 +0200
committerGitHub <noreply@github.com>2024-06-24 11:57:36 +0200
commitdfbfb6c5c6dba8a25c7a9769e969d56ba19fc14d (patch)
treea50b2ddf31a222b73d98cbdd4e13485a577455ab /clang/lib/Basic/SourceManager.cpp
parentf8ff9094711b74d3f695f7571f6390f8a481fc52 (diff)
downloadllvm-dfbfb6c5c6dba8a25c7a9769e969d56ba19fc14d.zip
llvm-dfbfb6c5c6dba8a25c7a9769e969d56ba19fc14d.tar.gz
llvm-dfbfb6c5c6dba8a25c7a9769e969d56ba19fc14d.tar.bz2
[SourceManager] Expose max usage of source location space as a Statistic (#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`.
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index f0af1a3..533a9fe 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -20,6 +20,7 @@
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Allocator.h"
@@ -46,6 +47,13 @@ using namespace clang;
using namespace SrcMgr;
using llvm::MemoryBuffer;
+#define DEBUG_TYPE "source-manager"
+
+// Reaching a limit of 2^31 results in a hard error. This metric allows to track
+// if particular invocation of the compiler is close to it.
+STATISTIC(MaxUsedSLocBytes, "Maximum number of bytes used by source locations "
+ "(both loaded and local).");
+
//===----------------------------------------------------------------------===//
// SourceManager Helper Classes
//===----------------------------------------------------------------------===//
@@ -466,6 +474,7 @@ SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
SLocEntryLoaded.resize(LoadedSLocEntryTable.size());
SLocEntryOffsetLoaded.resize(LoadedSLocEntryTable.size());
CurrentLoadedOffset -= TotalSize;
+ updateSlocUsageStats();
int BaseID = -int(LoadedSLocEntryTable.size()) - 1;
LoadedSLocEntryAllocBegin.push_back(FileID::get(BaseID));
return std::make_pair(BaseID, CurrentLoadedOffset);
@@ -619,6 +628,7 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
// We do a +1 here because we want a SourceLocation that means "the end of the
// file", e.g. for the "no newline at the end of the file" diagnostic.
NextLocalOffset += FileSize + 1;
+ updateSlocUsageStats();
// Set LastFileIDLookup to the newly created file. The next getFileID call is
// almost guaranteed to be from that file.
@@ -679,6 +689,7 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
}
// See createFileID for that +1.
NextLocalOffset += Length + 1;
+ updateSlocUsageStats();
return SourceLocation::getMacroLoc(NextLocalOffset - (Length + 1));
}
@@ -1843,6 +1854,12 @@ void SourceManager::associateFileChunkWithMacroArgExp(
MacroArgsCache[EndOffs] = EndOffsMappedLoc;
}
+void SourceManager::updateSlocUsageStats() const {
+ SourceLocation::UIntTy UsedBytes =
+ NextLocalOffset + (MaxLoadedOffset - CurrentLoadedOffset);
+ MaxUsedSLocBytes.updateMax(UsedBytes);
+}
+
/// If \arg Loc points inside a function macro argument, the returned
/// location will be the macro location in which the argument was expanded.
/// If a macro argument is used multiple times, the expanded location will