Commit 404fa0ab authored by Kevin Modzelewski's avatar Kevin Modzelewski

starting to get somewhere, though very slowly

parent d95030ec
......@@ -1077,7 +1077,7 @@ lint_%: %.cpp plugins/clang_linter.so
REFCOUNT_CHECKER_BUILD_PATH := $(CMAKE_DIR_DBG)/plugins/refcount_checker/llvm/bin/refcount_checker
REFCOUNT_CHECKER_RUN_PATH := $(CMAKE_DIR_DBG)/llvm/bin/refcount_checker
$(REFCOUNT_CHECKER_BUILD_PATH): plugins/refcount_checker/refcount_checker.cpp
$(REFCOUNT_CHECKER_RUN_PATH): plugins/refcount_checker/refcount_checker.cpp
$(NINJA) -C $(CMAKE_DIR_DBG) refcount_checker $(NINJAFLAGS)
cp $(REFCOUNT_CHECKER_BUILD_PATH) $(REFCOUNT_CHECKER_RUN_PATH)
......
......@@ -5,9 +5,7 @@ set(LLVM_LIBRARY_OUTPUT_INTDIR llvm/lib${LLVM_LIBDIR_SUFFIX})
# Pyston addition:
include_directories("${DEPS_DIR}/llvm-trunk/tools/clang/include")
include_directories("${CMAKE_BINARY_DIR}/llvm/tools/clang/include")
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(LIBUNWIND_DEBUG_CFLAGS "CFLAGS=-O0 -g")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
# From llvm-trunk/tools/clang/tools/clang-check/CMakeLists.txt
set(LLVM_LINK_COMPONENTS
......
......@@ -18,6 +18,7 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
......@@ -40,6 +41,130 @@ static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...");
static void dumpSingle(DeclContext* ctx) {
errs() << ctx->getDeclKindName() << '\n';
if (ctx->isClosure()) errs() << "a closure\n";
if (ctx->isFunctionOrMethod()) errs() << "a function / method\n";
//if (ctx->isLookupContext()) errs() << "a lookup context\n";
if (ctx->isFileContext()) errs() << "a file context\n";
if (ctx->isTranslationUnit()) errs() << "a translation unit\n";
if (ctx->isRecord()) errs() << "a record\n";
if (ctx->isNamespace()) errs() << "a namespace\n";
if (ctx->isStdNamespace()) errs() << "a std namespace\n";
if (ctx->isInlineNamespace()) errs() << "an inline namespace\n";
if (ctx->isDependentContext()) errs() << "a dependent context\n";
if (ctx->isTransparentContext()) errs() << "a transparent context\n";
if (ctx->isExternCContext()) errs() << "an extern-C context\n";
if (ctx->isExternCXXContext()) errs() << "an extern-C++ context\n";
//ctx->dumpLookups();
}
static void dump(DeclContext* ctx) {
auto cur = ctx;
while (cur) {
dumpSingle(cur);
cur = cur->getParent();
if (cur)
errs() << "parent is...\n";
}
}
namespace {
class ASTPrinter : public ASTConsumer,
public RecursiveASTVisitor<ASTPrinter> {
typedef RecursiveASTVisitor<ASTPrinter> base;
public:
ASTPrinter(raw_ostream *Out = nullptr, bool Dump = false,
StringRef FilterString = "", bool DumpLookups = false)
: Out(Out ? *Out : llvm::outs()), Dump(Dump),
FilterString(FilterString), DumpLookups(DumpLookups) {}
void HandleTranslationUnit(ASTContext &Context) override {
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
if (FilterString.empty())
return print(D);
TraverseDecl(D);
}
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool TraverseDecl(Decl *D) {
if (D && filterMatches(D)) {
bool ShowColors = Out.has_colors();
if (ShowColors)
Out.changeColor(raw_ostream::BLUE);
Out << ((Dump || DumpLookups) ? "Dumping " : "Printing ") << getName(D)
<< ":\n";
if (ShowColors)
Out.resetColor();
print(D);
Out << "\n";
// Don't traverse child nodes to avoid output duplication.
return true;
}
return base::TraverseDecl(D);
}
std::string getName(Decl *D) {
if (isa<NamedDecl>(D))
return cast<NamedDecl>(D)->getQualifiedNameAsString();
return "";
}
bool filterMatches(Decl *D) {
return getName(D).find(FilterString) != std::string::npos;
}
void print(Decl *D) {
if (DumpLookups) {
if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
if (DC == DC->getPrimaryContext())
DC->dumpLookups(Out, Dump);
else
Out << "Lookup map is in primary DeclContext "
<< DC->getPrimaryContext() << "\n";
} else
Out << "Not a DeclContext\n";
} else if (Dump)
D->dump(Out);
else
D->print(Out, /*Indentation=*/0, /*PrintInstantiation=*/true);
}
raw_ostream &Out;
bool Dump;
std::string FilterString;
bool DumpLookups;
};
class ASTDeclNodeLister : public ASTConsumer,
public RecursiveASTVisitor<ASTDeclNodeLister> {
public:
ASTDeclNodeLister(raw_ostream *Out = nullptr)
: Out(Out ? *Out : llvm::outs()) {}
void HandleTranslationUnit(ASTContext &Context) override {
TraverseDecl(Context.getTranslationUnitDecl());
}
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool VisitNamedDecl(NamedDecl *D) {
D->printQualifiedName(Out);
Out << '\n';
return true;
}
private:
raw_ostream &Out;
};
static std::unique_ptr<ASTPrinter> dumper() {
return std::unique_ptr<ASTPrinter>(new ASTPrinter(&errs(), true, "", true));
}
} // end anonymous namespace
class MyVisitor : public RecursiveASTVisitor<MyVisitor> {
private:
ASTContext *Context;
......@@ -51,9 +176,38 @@ public:
virtual ~MyVisitor() {
}
void checkFunction(FunctionDecl* func) {
//dumper()->TraverseDecl(func);
errs() << "printing:\n";
func->print(errs());
errs() << "dumping:\n";
func->dump(errs());
}
virtual bool VisitFunctionDecl(FunctionDecl* func) {
errs() << func->getNameInfo().getName() << '\n';
return true;
if (!func->hasBody())
return true /* keep going */;
auto filename = Context->getSourceManager().getFilename(func->getNameInfo().getLoc());
// Filter out functions defined in libraries:
if (filename.find("include/c++") != StringRef::npos)
return true;
if (filename.find("include/x86_64-linux-gnu") != StringRef::npos)
return true;
if (filename.find("include/llvm") != StringRef::npos)
return true;
if (filename.find("lib/clang") != StringRef::npos)
return true;
//errs() << filename << '\n';
if (func->getNameInfo().getAsString() != "firstlineno")
return true;
checkFunction(func);
return true /* keep going */;
}
};
......@@ -67,6 +221,7 @@ public:
virtual void HandleTranslationUnit(ASTContext &Context) {
visitor.TraverseDecl(Context.getTranslationUnitDecl());
//dumper()->TraverseDecl(Context.getTranslationUnitDecl());
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment