summaryrefslogtreecommitdiffstats
path: root/clang/lib/ASTMatchers/ASTMatchFinder.cpp
diff options
context:
space:
mode:
authorSamuel Benzaquen <sbenza@google.com>2014-10-22 20:31:05 +0000
committerSamuel Benzaquen <sbenza@google.com>2014-10-22 20:31:05 +0000
commit43dcf2172a01b2b7939427ace44d105ab0d0028f (patch)
tree36e6b35799de1dc3449f2ceee56b339d11a4b1d0 /clang/lib/ASTMatchers/ASTMatchFinder.cpp
parent2633341b4ae11b6a46c38896553105022af5f990 (diff)
downloadbcm5719-llvm-43dcf2172a01b2b7939427ace44d105ab0d0028f.tar.gz
bcm5719-llvm-43dcf2172a01b2b7939427ace44d105ab0d0028f.zip
Add support for profiling the matchers used.
Summary: Add support for profiling the matchers used. This will be connected with clang-tidy to generate a report to determine and debug slow checks. Reviewers: alexfh Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D5911 llvm-svn: 220418
Diffstat (limited to 'clang/lib/ASTMatchers/ASTMatchFinder.cpp')
-rw-r--r--clang/lib/ASTMatchers/ASTMatchFinder.cpp59
1 files changed, 52 insertions, 7 deletions
diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index a90eec00b47..612aaceaf23 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -20,7 +20,10 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Support/Timer.h"
#include <deque>
+#include <memory>
#include <set>
namespace clang {
@@ -292,17 +295,32 @@ private:
class MatchASTVisitor : public RecursiveASTVisitor<MatchASTVisitor>,
public ASTMatchFinder {
public:
- MatchASTVisitor(const MatchFinder::MatchersByType *Matchers)
- : Matchers(Matchers), ActiveASTContext(nullptr) {}
+ MatchASTVisitor(const MatchFinder::MatchersByType *Matchers,
+ const MatchFinder::MatchFinderOptions &Options)
+ : Matchers(Matchers), Options(Options), ActiveASTContext(nullptr) {}
+
+ ~MatchASTVisitor() {
+ if (Options.CheckProfiling) {
+ Options.CheckProfiling->Records = std::move(TimeByBucket);
+ }
+ }
void onStartOfTranslationUnit() {
- for (MatchCallback *MC : Matchers->AllCallbacks)
+ const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
+ for (MatchCallback *MC : Matchers->AllCallbacks) {
+ TimeRegion Timer(EnableCheckProfiling ? &TimeByBucket[MC->getID()]
+ : nullptr);
MC->onStartOfTranslationUnit();
+ }
}
void onEndOfTranslationUnit() {
- for (MatchCallback *MC : Matchers->AllCallbacks)
+ const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
+ for (MatchCallback *MC : Matchers->AllCallbacks) {
+ TimeRegion Timer(EnableCheckProfiling ? &TimeByBucket[MC->getID()]
+ : nullptr);
MC->onEndOfTranslationUnit();
+ }
}
void set_active_ast_context(ASTContext *NewActiveASTContext) {
@@ -471,12 +489,30 @@ public:
bool shouldUseDataRecursionFor(clang::Stmt *S) const { return false; }
private:
+ class TimeRegion {
+ public:
+ TimeRegion(llvm::TimeRecord *Record) : Record(Record) {
+ if (Record)
+ *Record -= llvm::TimeRecord::getCurrentTime(true);
+ }
+ ~TimeRegion() {
+ if (Record)
+ *Record += llvm::TimeRecord::getCurrentTime(false);
+ }
+
+ private:
+ llvm::TimeRecord *Record;
+ };
+
/// \brief Runs all the \p Matchers on \p Node.
///
/// Used by \c matchDispatch() below.
template <typename T, typename MC>
void matchImpl(const T &Node, const MC &Matchers) {
+ const bool EnableCheckProfiling = Options.CheckProfiling.hasValue();
for (const auto &MP : Matchers) {
+ TimeRegion Timer(EnableCheckProfiling ? &TimeByBucket[MP.second->getID()]
+ : nullptr);
BoundNodesTreeBuilder Builder;
if (MP.first.matches(Node, this, &Builder)) {
MatchVisitor Visitor(ActiveASTContext, MP.second);
@@ -627,7 +663,13 @@ private:
return false;
}
+ /// \brief Bucket to record map.
+ ///
+ /// Used to get the appropriate bucket for each matcher.
+ llvm::StringMap<llvm::TimeRecord> TimeByBucket;
+
const MatchFinder::MatchersByType *Matchers;
+ const MatchFinder::MatchFinderOptions &Options;
ASTContext *ActiveASTContext;
// Maps a canonical type to its TypedefDecls.
@@ -790,7 +832,8 @@ MatchFinder::MatchResult::MatchResult(const BoundNodes &Nodes,
MatchFinder::MatchCallback::~MatchCallback() {}
MatchFinder::ParsingDoneTestCallback::~ParsingDoneTestCallback() {}
-MatchFinder::MatchFinder() : ParsingDone(nullptr) {}
+MatchFinder::MatchFinder(MatchFinderOptions Options)
+ : Options(std::move(Options)), ParsingDone(nullptr) {}
MatchFinder::~MatchFinder() {}
@@ -860,13 +903,13 @@ std::unique_ptr<ASTConsumer> MatchFinder::newASTConsumer() {
void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node,
ASTContext &Context) {
- internal::MatchASTVisitor Visitor(&Matchers);
+ internal::MatchASTVisitor Visitor(&Matchers, Options);
Visitor.set_active_ast_context(&Context);
Visitor.match(Node);
}
void MatchFinder::matchAST(ASTContext &Context) {
- internal::MatchASTVisitor Visitor(&Matchers);
+ internal::MatchASTVisitor Visitor(&Matchers, Options);
Visitor.set_active_ast_context(&Context);
Visitor.onStartOfTranslationUnit();
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
@@ -878,5 +921,7 @@ void MatchFinder::registerTestCallbackAfterParsing(
ParsingDone = NewParsingDone;
}
+StringRef MatchFinder::MatchCallback::getID() const { return "<unknown>"; }
+
} // end namespace ast_matchers
} // end namespace clang
OpenPOWER on IntegriCloud