Skip to content

Commit 0ca0f58

Browse files
committed
Made MissingOriginCollector internal to Origins.cpp
1 parent 0081a95 commit 0ca0f58

File tree

5 files changed

+43
-53
lines changed

5 files changed

+43
-53
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ enum class Confidence : uint8_t {
3939
};
4040

4141
/// A structure to hold the statistics related to LifetimeAnalysis.
42-
/// Curently it holds only the missing origin details.
42+
/// Currently it holds only the missing origin details.
4343
struct LifetimeSafetyStats {
44+
/// A map from `ExpressionClassName<QualType>` to their missing origin
45+
/// counts.
4446
llvm::StringMap<unsigned> MissingOriginCount;
4547
};
4648

@@ -106,7 +108,6 @@ class LifetimeSafetyAnalysis {
106108
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
107109
};
108110
} // namespace internal
109-
110111
} // namespace clang::lifetimes
111112

112113
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ class OriginManager {
8787

8888
void dump(OriginID OID, llvm::raw_ostream &OS) const;
8989

90-
const llvm::DenseMap<const clang::Expr *, OriginID> &
91-
getExprToOriginId() const;
92-
90+
/// Collects statistics about expressions that lack associated origins.
9391
void collectMissingOrigins(Stmt *FunctionBody, LifetimeSafetyStats &LSStats);
9492

9593
private:
@@ -102,22 +100,6 @@ class OriginManager {
102100
llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
103101
llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
104102
};
105-
106-
/// An utility class to traverse the function body in the analysis
107-
/// context and collect the count of expressions with missing origins.
108-
class MissingOriginCollector
109-
: public RecursiveASTVisitor<MissingOriginCollector> {
110-
public:
111-
MissingOriginCollector(const OriginManager &om,
112-
llvm::StringMap<unsigned> &MissingOriginCount)
113-
: OM(om), MissingOriginCount(MissingOriginCount) {}
114-
bool VisitExpr(Expr *E);
115-
116-
private:
117-
const OriginManager &OM;
118-
llvm::StringMap<unsigned> &MissingOriginCount;
119-
};
120-
121103
} // namespace clang::lifetimes::internal
122104

123105
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H

clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,21 @@ void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
8484
void printStats(const LifetimeSafetyStats &Stats) {
8585
llvm::errs() << "\n*** LifetimeSafety Missing Origin Stats "
8686
"(expression_type : count) :\n\n";
87-
unsigned totalMissingOrigins = 0;
87+
unsigned TotalMissingOrigins = 0;
8888
for (const auto &[expr, count] : Stats.MissingOriginCount) {
8989
llvm::errs() << expr << " : " << count << '\n';
90-
totalMissingOrigins += count;
90+
TotalMissingOrigins += count;
9191
}
92-
llvm::errs() << "Total missing origins: " << totalMissingOrigins << "\n";
92+
llvm::errs() << "Total missing origins: " << TotalMissingOrigins << "\n";
9393
llvm::errs() << "\n****************************************\n";
9494
}
9595

9696
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC,
9797
LifetimeSafetyReporter *Reporter,
9898
LifetimeSafetyStats &Stats, bool CollectStats) {
99-
std::unique_ptr<internal::LifetimeSafetyAnalysis> Analysis =
100-
std::make_unique<internal::LifetimeSafetyAnalysis>(AC, Reporter);
101-
Analysis->run();
99+
internal::LifetimeSafetyAnalysis Analysis(AC, Reporter);
100+
Analysis.run();
102101
if (CollectStats)
103-
collectLifetimeStats(AC, Analysis->getFactManager().getOriginMgr(), Stats);
102+
collectLifetimeStats(AC, Analysis.getFactManager().getOriginMgr(), Stats);
104103
}
105104
} // namespace clang::lifetimes

clang/lib/Analysis/LifetimeSafety/Origins.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ static bool hasOrigin(const Expr *E) {
3434
return E->isGLValue() || isPointerType(E->getType());
3535
}
3636

37+
namespace {
38+
/// An utility class to traverse the function body in the analysis
39+
/// context and collect the count of expressions with missing origins.
40+
class MissingOriginCollector
41+
: public RecursiveASTVisitor<MissingOriginCollector> {
42+
public:
43+
MissingOriginCollector(
44+
const llvm::DenseMap<const clang::Expr *, OriginID> &ExprToOriginId,
45+
llvm::StringMap<unsigned> &MissingOriginCount)
46+
: ExprToOriginId(ExprToOriginId), MissingOriginCount(MissingOriginCount) {
47+
}
48+
bool VisitExpr(Expr *E) {
49+
if (!hasOrigin(E))
50+
return true;
51+
// Check if we have an origin for this expression.
52+
auto It = this->ExprToOriginId.find(E);
53+
if (It == this->ExprToOriginId.end()) {
54+
// No origin found: count this as missing origin.
55+
std::string ExprStr = std::string(E->getStmtClassName()) +
56+
" (Type: " + E->getType().getAsString() + ")";
57+
MissingOriginCount[ExprStr]++;
58+
}
59+
return true;
60+
}
61+
62+
private:
63+
const llvm::DenseMap<const clang::Expr *, OriginID> &ExprToOriginId;
64+
llvm::StringMap<unsigned> &MissingOriginCount;
65+
};
66+
} // namespace
67+
3768
void OriginManager::dump(OriginID OID, llvm::raw_ostream &OS) const {
3869
OS << OID << " (";
3970
Origin O = getOrigin(OID);
@@ -63,7 +94,6 @@ OriginID OriginManager::get(const Expr &E) {
6394
auto It = ExprToOriginID.find(&E);
6495
if (It != ExprToOriginID.end())
6596
return It->second;
66-
6797
// If the expression itself has no specific origin, and it's a reference
6898
// to a declaration, its origin is that of the declaration it refers to.
6999
// For pointer types, where we don't pre-emptively create an origin for the
@@ -113,33 +143,14 @@ OriginID OriginManager::getOrCreate(const ValueDecl &D) {
113143
return NewID;
114144
}
115145

116-
const llvm::DenseMap<const clang::Expr *, OriginID> &
117-
OriginManager::getExprToOriginId() const {
118-
return ExprToOriginID;
119-
}
120-
121146
void OriginManager::collectMissingOrigins(Stmt *FunctionBody,
122147
LifetimeSafetyStats &LSStats) {
123148
if (!FunctionBody)
124149
return;
125150

126-
MissingOriginCollector Collector(*this, LSStats.MissingOriginCount);
151+
MissingOriginCollector Collector(this->ExprToOriginID,
152+
LSStats.MissingOriginCount);
127153
Collector.TraverseStmt(const_cast<Stmt *>(FunctionBody));
128154
}
129155

130-
bool MissingOriginCollector::VisitExpr(Expr *E) {
131-
if (!hasOrigin(E))
132-
return true;
133-
// Check if we have an origin for this expression
134-
const auto &ExprToOriginId = OM.getExprToOriginId();
135-
auto It = ExprToOriginId.find(E);
136-
if (It == ExprToOriginId.end()) {
137-
// No origin found - count this as missing
138-
std::string ExprStr = std::string(E->getStmtClassName()) + "<" +
139-
E->getType().getAsString() + ">";
140-
MissingOriginCount[ExprStr]++;
141-
}
142-
return true;
143-
}
144-
145156
} // namespace clang::lifetimes::internal

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
3030
#include "clang/Analysis/Analyses/CalledOnceCheck.h"
3131
#include "clang/Analysis/Analyses/Consumed.h"
32-
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
3332
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
34-
#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
3533
#include "clang/Analysis/Analyses/ReachableCode.h"
3634
#include "clang/Analysis/Analyses/ThreadSafety.h"
3735
#include "clang/Analysis/Analyses/UninitializedValues.h"
@@ -49,7 +47,6 @@
4947
#include "clang/Sema/SemaInternal.h"
5048
#include "llvm/ADT/ArrayRef.h"
5149
#include "llvm/ADT/BitVector.h"
52-
#include "llvm/ADT/DenseMap.h"
5350
#include "llvm/ADT/MapVector.h"
5451
#include "llvm/ADT/STLFunctionalExtras.h"
5552
#include "llvm/ADT/SmallVector.h"

0 commit comments

Comments
 (0)