Skip to content

Commit 8264809

Browse files
committed
Separating the StmtClassName and QualType for missing origin counts and putting them in separate maps
1 parent a30fa2e commit 8264809

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ struct LifetimeFactory {
7474
/// Running the lifetime safety analysis and querying its results. It
7575
/// encapsulates the various dataflow analyses.
7676
class LifetimeSafetyAnalysis {
77-
7877
public:
7978
LifetimeSafetyAnalysis(AnalysisDeclContext &AC,
8079
LifetimeSafetyReporter *Reporter);
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
//===- LifetimeStats.h - Miscellaneous statistics related to lifetime safety
2-
//analysis ---*- C++ -*-===//
1+
//===- LifetimeStats.h - Lifetime Safety Statistics --------------*- C++-* -===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
65
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
8-
//===----------------------------------------------------------------------------------------------===//
7+
//===------------------------------------------------------------------------===//
98
//
109
// This file declares the data structures and utility function for collection of
11-
// staticstics related to Lifetimesafety analysis.
10+
// statistics related to Lifetime Safety analysis.
1211
//
13-
//===----------------------------------------------------------------------------------------------===//
12+
//===------------------------------------------------------------------------===//
1413

1514
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIFETIMESTATS_H
1615
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIFETIMESTATS_H
@@ -19,15 +18,17 @@
1918

2019
namespace clang::lifetimes {
2120
/// A structure to hold the statistics related to LifetimeAnalysis.
22-
/// Currently it holds only the missing origin details.
21+
/// These are accumulated across all analyzed functions and printed
22+
/// when -print-stats is enabled.
2323
struct LifetimeSafetyStats {
24-
/// A map from `ExpressionClassName<QualType>` to their missing origin
25-
/// counts.
26-
llvm::StringMap<unsigned> MissingOriginCount;
24+
/// A map from `QualType` to their missing origin counts.
25+
llvm::StringMap<unsigned> ExprStmtClassToMissingOriginCount;
26+
/// A map from `StmtClassName` to their missing origin counts.
27+
llvm::StringMap<unsigned> ExprTypeToMissingOriginCount;
2728
};
2829

29-
// utility function to print missing origin stats.
30+
/// Utility function to print missing origin stats.
3031
void printStats(const LifetimeSafetyStats &Stats);
3132
} // namespace clang::lifetimes
3233

33-
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIFETIMESTATS_H
34+
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIFETIMESTATS_H

clang/lib/Analysis/LifetimeSafety/LifetimeStats.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//===- LifetimeStats.cpp - Miscellaneous statistics related to C++ Lifetime
2-
//Safety analysis -*--------- C++-*-===//
1+
//===- LifetimeStats.cpp - Lifetime Safety Statistics -*------------ C++-*-===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
@@ -17,14 +16,19 @@
1716

1817
namespace clang::lifetimes {
1918
void printStats(const LifetimeSafetyStats &Stats) {
20-
llvm::errs() << "\n*** LifetimeSafety Missing Origin Stats "
21-
"(expression_type : count) :\n\n";
19+
llvm::errs() << "\n*** LifetimeSafety Missing Origin per QualType: "
20+
"(QualType : count) :\n\n";
2221
unsigned TotalMissingOrigins = 0;
23-
for (const auto &[expr, count] : Stats.MissingOriginCount) {
24-
llvm::errs() << expr << " : " << count << '\n';
22+
for (const auto &[type, count] : Stats.ExprTypeToMissingOriginCount) {
23+
llvm::errs() << type << " : " << count << '\n';
2524
TotalMissingOrigins += count;
2625
}
27-
llvm::errs() << "Total missing origins: " << TotalMissingOrigins << "\n";
26+
llvm::errs() << "\n\n*** LifetimeSafety Missing Origin per StmtClassName: "
27+
"(StmtClassName : count) :\n\n";
28+
for (const auto &[stmt, count] : Stats.ExprStmtClassToMissingOriginCount) {
29+
llvm::errs() << stmt << " : " << count << '\n';
30+
}
31+
llvm::errs() << "\nTotal missing origins: " << TotalMissingOrigins << "\n";
2832
llvm::errs() << "\n****************************************\n";
2933
}
30-
} // namespace clang::lifetimes
34+
} // namespace clang::lifetimes

clang/lib/Analysis/LifetimeSafety/Origins.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,34 @@ bool isPointerType(QualType QT) {
2323
bool hasOrigin(const Expr *E) {
2424
return E->isGLValue() || isPointerType(E->getType());
2525
}
26-
/// An utility class to traverse the function body in the analysis
26+
/// A utility class to traverse the function body in the analysis
2727
/// context and collect the count of expressions with missing origins.
2828
class MissingOriginCollector
2929
: public RecursiveASTVisitor<MissingOriginCollector> {
3030
public:
3131
MissingOriginCollector(
3232
const llvm::DenseMap<const clang::Expr *, OriginID> &ExprToOriginId,
33-
llvm::StringMap<unsigned> &MissingOriginCount)
34-
: ExprToOriginId(ExprToOriginId), MissingOriginCount(MissingOriginCount) {
35-
}
33+
llvm::StringMap<unsigned> &ExprStmtClassToMissingOriginCount,
34+
llvm::StringMap<unsigned> &ExprTypeToMissingOriginCount)
35+
: ExprToOriginId(ExprToOriginId),
36+
ExprStmtClassToMissingOriginCount(ExprStmtClassToMissingOriginCount),
37+
ExprTypeToMissingOriginCount(ExprTypeToMissingOriginCount) {}
3638
bool VisitExpr(Expr *E) {
3739
if (!hasOrigin(E))
3840
return true;
3941
// Check if we have an origin for this expression.
40-
auto It = this->ExprToOriginId.find(E);
41-
if (It == this->ExprToOriginId.end()) {
42+
if (!ExprToOriginId.contains(E)) {
4243
// No origin found: count this as missing origin.
43-
std::string ExprStr = std::string(E->getStmtClassName()) +
44-
" (Type: " + E->getType().getAsString() + ")";
45-
MissingOriginCount[ExprStr]++;
44+
ExprTypeToMissingOriginCount[std::string(E->getType().getAsString())]++;
45+
ExprStmtClassToMissingOriginCount[std::string(E->getStmtClassName())]++;
4646
}
4747
return true;
4848
}
4949

5050
private:
5151
const llvm::DenseMap<const clang::Expr *, OriginID> &ExprToOriginId;
52-
llvm::StringMap<unsigned> &MissingOriginCount;
52+
llvm::StringMap<unsigned> &ExprStmtClassToMissingOriginCount;
53+
llvm::StringMap<unsigned> &ExprTypeToMissingOriginCount;
5354
};
5455
} // namespace
5556

@@ -137,7 +138,8 @@ void OriginManager::collectMissingOrigins(Stmt *FunctionBody,
137138
return;
138139

139140
MissingOriginCollector Collector(this->ExprToOriginID,
140-
LSStats.MissingOriginCount);
141+
LSStats.ExprStmtClassToMissingOriginCount,
142+
LSStats.ExprTypeToMissingOriginCount);
141143
Collector.TraverseStmt(const_cast<Stmt *>(FunctionBody));
142144
}
143145

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
#include "clang/Sema/SemaInternal.h"
4848
#include "llvm/ADT/ArrayRef.h"
4949
#include "llvm/ADT/BitVector.h"
50+
#include "llvm/ADT/DenseMap.h"
5051
#include "llvm/ADT/MapVector.h"
5152
#include "llvm/ADT/STLFunctionalExtras.h"
5253
#include "llvm/ADT/SmallVector.h"
5354
#include "llvm/ADT/StringRef.h"
5455
#include "llvm/Support/Debug.h"
55-
#include "llvm/Support/raw_ostream.h"
5656
#include <algorithm>
5757
#include <deque>
5858
#include <iterator>

0 commit comments

Comments
 (0)