Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EntryPointStat {
public:
llvm::StringLiteral name() const { return Name; }

static void lockRegistry();
static void lockRegistry(llvm::StringRef CPPFileName);

static void takeSnapshot(const Decl *EntryPoint);
static void dumpStatsAsCSV(llvm::raw_ostream &OS);
Expand Down
31 changes: 24 additions & 7 deletions clang/lib/StaticAnalyzer/Core/EntryPointStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "clang/StaticAnalyzer/Core/PathSensitive/EntryPointStats.h"
#include "clang/AST/DeclBase.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -38,6 +40,7 @@ struct Registry {
};

std::vector<Snapshot> Snapshots;
std::string EscapedCPPFileName;
};
} // namespace

Expand Down Expand Up @@ -69,7 +72,7 @@ static void checkStatName(const EntryPointStat *M) {
}
}

void EntryPointStat::lockRegistry() {
void EntryPointStat::lockRegistry(llvm::StringRef CPPFileName) {
auto CmpByNames = [](const EntryPointStat *L, const EntryPointStat *R) {
return L->name() < R->name();
};
Expand All @@ -78,6 +81,8 @@ void EntryPointStat::lockRegistry() {
enumerateStatVectors(
[](const auto &Stats) { llvm::for_each(Stats, checkStatName); });
StatsRegistry->IsLocked = true;
llvm::raw_string_ostream OS(StatsRegistry->EscapedCPPFileName);
llvm::printEscapedString(CPPFileName, OS);
}

[[maybe_unused]] static bool isRegistered(llvm::StringLiteral Name) {
Expand Down Expand Up @@ -144,15 +149,27 @@ static std::vector<llvm::StringLiteral> getStatNames() {
return Ret;
}

static std::string getUSR(const Decl *D) {
llvm::SmallVector<char> Buf;
if (index::generateUSRForDecl(D, Buf)) {
assert(false && "This should never fail");
return AnalysisDeclContext::getFunctionName(D);
}
return llvm::toStringRef(Buf).str();
}

void Registry::Snapshot::dumpAsCSV(llvm::raw_ostream &OS) const {
OS << '"';
llvm::printEscapedString(getUSR(EntryPoint), OS);
OS << "\",\"";
OS << StatsRegistry->EscapedCPPFileName << "\",\"";
llvm::printEscapedString(
clang::AnalysisDeclContext::getFunctionName(EntryPoint), OS);
OS << "\", ";
OS << "\",";
auto PrintAsBool = [&OS](bool B) { OS << (B ? "true" : "false"); };
llvm::interleaveComma(BoolStatValues, OS, PrintAsBool);
OS << ((BoolStatValues.empty() || UnsignedStatValues.empty()) ? "" : ", ");
llvm::interleaveComma(UnsignedStatValues, OS);
llvm::interleave(BoolStatValues, OS, PrintAsBool, ",");
OS << ((BoolStatValues.empty() || UnsignedStatValues.empty()) ? "" : ",");
llvm::interleave(UnsignedStatValues, OS, [&OS](unsigned U) { OS << U; }, ",");
}

static std::vector<bool> consumeBoolStats() {
Expand Down Expand Up @@ -181,8 +198,8 @@ void EntryPointStat::dumpStatsAsCSV(llvm::StringRef FileName) {
}

void EntryPointStat::dumpStatsAsCSV(llvm::raw_ostream &OS) {
OS << "EntryPoint, ";
llvm::interleaveComma(getStatNames(), OS);
OS << "USR,File,DebugName,";
llvm::interleave(getStatNames(), OS, [&OS](const auto &a) { OS << a; }, ",");
OS << "\n";

std::vector<std::string> Rows;
Expand Down
12 changes: 11 additions & 1 deletion clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ STAT_MAX(MaxCFGSize, "The maximum number of basic blocks in a function.");

namespace {

StringRef getMainFileName(const CompilerInvocation &Invocation) {
if (!Invocation.getFrontendOpts().Inputs.empty()) {
const FrontendInputFile &Input = Invocation.getFrontendOpts().Inputs[0];
return Input.isFile() ? Input.getFile()
: Input.getBuffer().getBufferIdentifier();
}
return {};
Copy link
Contributor

@NagyDonat NagyDonat Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about returning a human-readable placeholder like "<no input>" in this case? I would guess that it would be more helpful (e.g. easier to search for) if someone runs into this rare corner case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense changed in 7118400

}

class AnalysisConsumer : public AnalysisASTConsumer,
public DynamicRecursiveASTVisitor {
enum {
Expand Down Expand Up @@ -125,7 +134,8 @@ class AnalysisConsumer : public AnalysisASTConsumer,
PP(CI.getPreprocessor()), OutDir(outdir), Opts(opts), Plugins(plugins),
Injector(std::move(injector)), CTU(CI),
MacroExpansions(CI.getLangOpts()) {
EntryPointStat::lockRegistry();

EntryPointStat::lockRegistry(getMainFileName(CI.getInvocation()));
DigestAnalyzerOptions();

if (Opts.AnalyzerDisplayProgress || Opts.PrintStats ||
Expand Down
8 changes: 6 additions & 2 deletions clang/test/Analysis/analyzer-stats/entry-point-stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// RUN: %csv2json "%t.csv" | FileCheck --check-prefix=CHECK %s
//
// CHECK: {
// CHECK-NEXT: "fib(unsigned int)": {
// CHECK-NEXT: "c:@F@fib#i#": {
// CHECK-NEXT: "File": "{{.*}}entry-point-stats.cpp",
// CHECK-NEXT: "DebugName": "fib(unsigned int)",
// CHECK-NEXT: "NumBlocks": "{{[0-9]+}}",
// CHECK-NEXT: "NumBlocksUnreachable": "{{[0-9]+}}",
// CHECK-NEXT: "NumCTUSteps": "{{[0-9]+}}",
Expand Down Expand Up @@ -40,7 +42,9 @@
// CHECK-NEXT: "MaxValidBugClassSize": "{{[0-9]+}}",
// CHECK-NEXT: "PathRunningTime": "{{[0-9]+}}"
// CHECK-NEXT: },
// CHECK-NEXT: "main(int, char **)": {
// CHECK-NEXT: "c:@F@main#I#**C#": {
// CHECK-NEXT: "File": "{{.*}}entry-point-stats.cpp",
// CHECK-NEXT: "DebugName": "main(int, char **)",
// CHECK-NEXT: "NumBlocks": "{{[0-9]+}}",
// CHECK-NEXT: "NumBlocksUnreachable": "{{[0-9]+}}",
// CHECK-NEXT: "NumCTUSteps": "{{[0-9]+}}",
Expand Down