Skip to content

Commit feeb663

Browse files
committed
nested namespaces
1 parent 98bb91d commit feeb663

File tree

21 files changed

+190
-143
lines changed

21 files changed

+190
-143
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
2020
#include "clang/Analysis/Analyses/LifetimeSafety/Reporter.h"
2121

22-
namespace clang {
23-
namespace lifetimes {
24-
namespace internal {
22+
namespace clang::lifetimes::internal {
2523

2624
/// Runs the lifetime checker, which detects use-after-free errors by
2725
/// examining loan expiration points and checking if any live origins hold
@@ -31,8 +29,6 @@ void runLifetimeChecker(LoanPropagationAnalysis &LoanPropagation,
3129
AnalysisDeclContext &ADC,
3230
LifetimeSafetyReporter *Reporter);
3331

34-
} // namespace internal
35-
} // namespace lifetimes
36-
} // namespace clang
32+
} // namespace clang::lifetimes::internal
3733

3834
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_CHECKER_H

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include "llvm/Support/TimeProfiler.h"
2525
#include <optional>
2626

27-
namespace clang::lifetimes {
28-
namespace internal {
27+
namespace clang::lifetimes::internal {
2928

3029
enum class Direction { Forward, Backward };
3130

@@ -185,6 +184,5 @@ class DataflowAnalysis {
185184
Lattice transfer(Lattice In, const UseFact &) { return In; }
186185
Lattice transfer(Lattice In, const TestPointFact &) { return In; }
187186
};
188-
} // namespace internal
189-
} // namespace clang::lifetimes
187+
} // namespace clang::lifetimes::internal
190188
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H

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

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
#include "clang/Analysis/Analyses/LifetimeSafety/Loans.h"
1818
#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
19-
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
2019
#include "clang/Analysis/AnalysisDeclContext.h"
2120
#include "clang/Analysis/CFG.h"
2221
#include "llvm/ADT/SmallVector.h"
2322
#include "llvm/Support/Debug.h"
2423
#include <cstdint>
2524

26-
namespace clang::lifetimes {
27-
namespace internal {
25+
namespace clang::lifetimes::internal {
2826
/// An abstract base class for a single, atomic lifetime-relevant event.
2927
class Fact {
3028

@@ -65,9 +63,7 @@ class Fact {
6563
}
6664

6765
virtual void dump(llvm::raw_ostream &OS, const LoanManager &,
68-
const OriginManager &) const {
69-
OS << "Fact (Kind: " << static_cast<int>(K) << ")\n";
70-
}
66+
const OriginManager &) const;
7167
};
7268

7369
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
@@ -88,13 +84,7 @@ class IssueFact : public Fact {
8884
LoanID getLoanID() const { return LID; }
8985
OriginID getOriginID() const { return OID; }
9086
void dump(llvm::raw_ostream &OS, const LoanManager &LM,
91-
const OriginManager &OM) const override {
92-
OS << "Issue (";
93-
LM.getLoan(getLoanID()).dump(OS);
94-
OS << ", ToOrigin: ";
95-
OM.dump(getOriginID(), OS);
96-
OS << ")\n";
97-
}
87+
const OriginManager &OM) const override;
9888
};
9989

10090
class ExpireFact : public Fact {
@@ -111,11 +101,7 @@ class ExpireFact : public Fact {
111101
SourceLocation getExpiryLoc() const { return ExpiryLoc; }
112102

113103
void dump(llvm::raw_ostream &OS, const LoanManager &LM,
114-
const OriginManager &) const override {
115-
OS << "Expire (";
116-
LM.getLoan(getLoanID()).dump(OS);
117-
OS << ")\n";
118-
}
104+
const OriginManager &) const override;
119105
};
120106

121107
class OriginFlowFact : public Fact {
@@ -139,14 +125,7 @@ class OriginFlowFact : public Fact {
139125
bool getKillDest() const { return KillDest; }
140126

141127
void dump(llvm::raw_ostream &OS, const LoanManager &,
142-
const OriginManager &OM) const override {
143-
OS << "OriginFlow (Dest: ";
144-
OM.dump(getDestOriginID(), OS);
145-
OS << ", Src: ";
146-
OM.dump(getSrcOriginID(), OS);
147-
OS << (getKillDest() ? "" : ", Merge");
148-
OS << ")\n";
149-
}
128+
const OriginManager &OM) const override;
150129
};
151130

152131
class ReturnOfOriginFact : public Fact {
@@ -160,11 +139,7 @@ class ReturnOfOriginFact : public Fact {
160139
ReturnOfOriginFact(OriginID OID) : Fact(Kind::ReturnOfOrigin), OID(OID) {}
161140
OriginID getReturnedOriginID() const { return OID; }
162141
void dump(llvm::raw_ostream &OS, const LoanManager &,
163-
const OriginManager &OM) const override {
164-
OS << "ReturnOfOrigin (";
165-
OM.dump(getReturnedOriginID(), OS);
166-
OS << ")\n";
167-
}
142+
const OriginManager &OM) const override;
168143
};
169144

170145
class UseFact : public Fact {
@@ -187,11 +162,7 @@ class UseFact : public Fact {
187162
bool isWritten() const { return IsWritten; }
188163

189164
void dump(llvm::raw_ostream &OS, const LoanManager &,
190-
const OriginManager &OM) const override {
191-
OS << "Use (";
192-
OM.dump(getUsedOrigin(OM), OS);
193-
OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
194-
}
165+
const OriginManager &OM) const override;
195166
};
196167

197168
/// A dummy-fact used to mark a specific point in the code for testing.
@@ -208,9 +179,7 @@ class TestPointFact : public Fact {
208179
StringRef getAnnotation() const { return Annotation; }
209180

210181
void dump(llvm::raw_ostream &OS, const LoanManager &,
211-
const OriginManager &) const override {
212-
OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
213-
}
182+
const OriginManager &) const override;
214183
};
215184

216185
class FactManager {
@@ -233,26 +202,7 @@ class FactManager {
233202
return new (Mem) FactType(std::forward<Args>(args)...);
234203
}
235204

236-
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
237-
llvm::dbgs() << "==========================================\n";
238-
llvm::dbgs() << " Lifetime Analysis Facts:\n";
239-
llvm::dbgs() << "==========================================\n";
240-
if (const Decl *D = AC.getDecl())
241-
if (const auto *ND = dyn_cast<NamedDecl>(D))
242-
llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n";
243-
// Print blocks in the order as they appear in code for a stable ordering.
244-
for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) {
245-
llvm::dbgs() << " Block B" << B->getBlockID() << ":\n";
246-
auto It = BlockToFactsMap.find(B);
247-
if (It != BlockToFactsMap.end()) {
248-
for (const Fact *F : It->second) {
249-
llvm::dbgs() << " ";
250-
F->dump(llvm::dbgs(), LoanMgr, OriginMgr);
251-
}
252-
}
253-
llvm::dbgs() << " End of Block\n";
254-
}
255-
}
205+
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const;
256206

257207
LoanManager &getLoanMgr() { return LoanMgr; }
258208
const LoanManager &getLoanMgr() const { return LoanMgr; }
@@ -266,7 +216,6 @@ class FactManager {
266216
BlockToFactsMap;
267217
llvm::BumpPtrAllocator FactAllocator;
268218
};
269-
} // namespace internal
270-
} // namespace clang::lifetimes
219+
} // namespace clang::lifetimes::internal
271220

272221
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include "clang/Analysis/CFG.h"
2323
#include "llvm/ADT/SmallVector.h"
2424

25-
namespace clang::lifetimes {
26-
namespace internal {
25+
namespace clang::lifetimes::internal {
2726

2827
class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
2928
using Base = ConstStmtVisitor<FactsGenerator>;
@@ -102,7 +101,6 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
102101
llvm::DenseMap<const DeclRefExpr *, UseFact *> UseFacts;
103102
};
104103

105-
} // namespace internal
106-
} // namespace clang::lifetimes
104+
} // namespace clang::lifetimes::internal
107105

108106
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTSGENERATOR_H

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,19 @@ class LifetimeSafetyAnalysis {
5252

5353
void run();
5454

55-
/// Returns the set of loans an origin holds at a specific program point.
56-
LoanSet getLoansAtPoint(OriginID OID, ProgramPoint PP) const;
55+
/// Returns the loan propagation analysis object.
56+
/// \note This is intended for testing only.
57+
LoanPropagationAnalysis &getLoanPropagationAnalysis() const {
58+
assert(LoanPropagation && "Analysis has not been run.");
59+
return *LoanPropagation;
60+
}
61+
62+
/// Returns the live origin analysis object.
63+
/// \note This is intended for testing only.
64+
LiveOriginAnalysis &getLiveOriginAnalysis() const {
65+
assert(LiveOrigins && "Analysis has not been run.");
66+
return *LiveOrigins;
67+
}
5768

5869
/// Returns the set of origins that are live at a specific program point,
5970
/// along with the confidence level of their liveness.

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include "llvm/ADT/ImmutableMap.h"
3030
#include "llvm/Support/Debug.h"
3131

32-
namespace clang::lifetimes {
33-
namespace internal {
32+
namespace clang::lifetimes::internal {
3433

3534
using OriginSet = llvm::ImmutableSet<OriginID>;
3635

@@ -132,7 +131,6 @@ class LiveOriginAnalysis
132131
FactManager &FactMgr;
133132
LivenessMap::Factory &Factory;
134133
};
135-
} // namespace internal
136-
} // namespace clang::lifetimes
134+
} // namespace clang::lifetimes::internal
137135

138136
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
#include "llvm/ADT/ImmutableSet.h"
2424
#include "llvm/Support/Debug.h"
2525

26-
namespace clang::lifetimes {
27-
namespace internal {
26+
namespace clang::lifetimes::internal {
2827

2928
// Using LLVM's immutable collections is efficient for dataflow analysis
3029
// as it avoids deep copies during state transitions.
@@ -91,7 +90,6 @@ class LoanPropagationAnalysis
9190
return LoanSetFactory.getEmptySet();
9291
}
9392
};
94-
} // namespace internal
95-
} // namespace clang::lifetimes
93+
} // namespace clang::lifetimes::internal
9694

9795
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
1919
#include "llvm/Support/raw_ostream.h"
2020

21-
namespace clang::lifetimes {
22-
namespace internal {
21+
namespace clang::lifetimes::internal {
2322

2423
using LoanID = utils::ID<struct LoanTag>;
2524
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
@@ -49,10 +48,7 @@ struct Loan {
4948
Loan(LoanID id, AccessPath path, const Expr *IssueExpr)
5049
: ID(id), Path(path), IssueExpr(IssueExpr) {}
5150

52-
void dump(llvm::raw_ostream &OS) const {
53-
OS << ID << " (Path: ";
54-
OS << Path.D->getNameAsString() << ")";
55-
}
51+
void dump(llvm::raw_ostream &OS) const;
5652
};
5753

5854
/// Manages the creation, storage and retrieval of loans.
@@ -79,7 +75,6 @@ class LoanManager {
7975
/// optimisation.
8076
llvm::SmallVector<Loan> AllLoans;
8177
};
82-
} // namespace internal
83-
} // namespace clang::lifetimes
78+
} // namespace clang::lifetimes::internal
8479

8580
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#include "clang/AST/Expr.h"
1919
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
2020

21-
namespace clang::lifetimes {
22-
namespace internal {
21+
namespace clang::lifetimes::internal {
2322

2423
using OriginID = utils::ID<struct OriginTag>;
2524
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
@@ -123,17 +122,7 @@ class OriginManager {
123122
return NewID;
124123
}
125124

126-
void dump(OriginID OID, llvm::raw_ostream &OS) const {
127-
OS << OID << " (";
128-
Origin O = getOrigin(OID);
129-
if (const ValueDecl *VD = O.getDecl())
130-
OS << "Decl: " << VD->getNameAsString();
131-
else if (const Expr *E = O.getExpr())
132-
OS << "Expr: " << E->getStmtClassName();
133-
else
134-
OS << "Unknown";
135-
OS << ")";
136-
}
125+
void dump(OriginID OID, llvm::raw_ostream &OS) const;
137126

138127
private:
139128
OriginID getNextOriginID() { return NextOriginID++; }
@@ -145,7 +134,6 @@ class OriginManager {
145134
llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
146135
llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
147136
};
148-
} // namespace internal
149-
} // namespace clang::lifetimes
137+
} // namespace clang::lifetimes::internal
150138

151139
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
#include "llvm/ADT/ImmutableMap.h"
1515
#include "llvm/ADT/ImmutableSet.h"
1616

17-
namespace clang::lifetimes {
18-
namespace internal {
19-
20-
namespace utils {
17+
namespace clang::lifetimes::internal::utils {
2118

2219
/// A generic, type-safe wrapper for an ID, distinguished by its `Tag` type.
2320
/// Used for giving ID to loans and origins.
@@ -95,9 +92,7 @@ join(const llvm::ImmutableMap<K, V> &A, const llvm::ImmutableMap<K, V> &B,
9592
}
9693
return Res;
9794
}
98-
} // namespace utils
99-
} // namespace internal
100-
} // namespace clang::lifetimes
95+
} // namespace clang::lifetimes::internal::utils
10196

10297
namespace llvm {
10398
template <typename Tag>

0 commit comments

Comments
 (0)