summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRalf Habacker <ralf.habacker@freenet.de>2016-09-13 01:00:08 (GMT)
committerRalf Habacker <ralf.habacker@freenet.de>2016-09-13 01:00:08 (GMT)
commit3abff2a605bf2c1a6991017cb5b41ef404fd3ec6 (patch)
tree2feb73aa9d7bfc0dca38b5defb5b439bf84d29a0
parent3980eaaf5cb7a696c8c71c9fafbba35fe20144db (diff)
Add llvm parser test case.
The test case is based on the example at http://clang.llvm.org/docs/RAVFrontendAction.html and parses a default string or a complete file provided as first command line parameter.
-rw-r--r--unittests/CMakeLists.txt4
-rw-r--r--unittests/TEST_llvm_parser.cpp101
2 files changed, 105 insertions, 0 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index 2c3352e..dbf96a2 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -130,4 +130,8 @@ if(LLVM_FOUND)
# Link against LLVM libraries
target_link_libraries(test-llvm ${llvm_libs} clangFrontend clangTooling)
+
+ add_executable(TEST_llvm_parser TEST_llvm_parser.cpp)
+ llvm_map_components_to_libnames(llvm_libs support)
+ target_link_libraries(TEST_llvm_parser ${llvm_libs} clangFrontend clangTooling clangAST clangBasic ${LIBS})
endif()
diff --git a/unittests/TEST_llvm_parser.cpp b/unittests/TEST_llvm_parser.cpp
new file mode 100644
index 0000000..122d823
--- /dev/null
+++ b/unittests/TEST_llvm_parser.cpp
@@ -0,0 +1,101 @@
+/*
+ Copyright 2016 Ralf Habacker <ralf.habacker@freenet.de>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License or (at your option) version 3 or any later version
+ accepted by the membership of KDE e.V. (or its successor approved
+ by the membership of KDE e.V.), which shall act as a proxy
+ defined in Section 14 of version 3 of the license.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendAction.h"
+#include "clang/Tooling/Tooling.h"
+
+#include <QFile>
+#include <QString>
+
+using namespace clang;
+
+class FindNamedClassVisitor : public RecursiveASTVisitor<FindNamedClassVisitor>
+{
+public:
+ explicit FindNamedClassVisitor(ASTContext *Context)
+ : Context(Context)
+ {
+ }
+
+ bool VisitStmt(Stmt *s)
+ {
+ s->dump();
+ return true;
+ }
+
+ bool VisitCXXRecordDecl(CXXRecordDecl *Declaration)
+ {
+ if (Declaration->getQualifiedNameAsString() == "n::m::C") {
+ FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getLocStart());
+ if (FullLocation.isValid())
+ llvm::outs() << "Found declaration at "
+ << FullLocation.getSpellingLineNumber() << ":"
+ << FullLocation.getSpellingColumnNumber() << "\n";
+ }
+ return true;
+ }
+
+private:
+ ASTContext *Context;
+};
+
+class FindNamedClassConsumer : public clang::ASTConsumer
+{
+public:
+ explicit FindNamedClassConsumer(ASTContext *Context)
+ : Visitor(Context)
+ {
+ }
+
+ virtual void HandleTranslationUnit(clang::ASTContext &Context)
+ {
+ Visitor.TraverseDecl(Context.getTranslationUnitDecl());
+ }
+
+private:
+ FindNamedClassVisitor Visitor;
+};
+
+class FindNamedClassAction : public clang::ASTFrontendAction
+{
+public:
+ virtual std::unique_ptr<clang::ASTConsumer>
+ CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile)
+ {
+ return std::unique_ptr<clang::ASTConsumer>(
+ new FindNamedClassConsumer(&Compiler.getASTContext()));
+ }
+};
+
+int main(int argc, char **argv)
+{
+ QByteArray defaultSource = "namespace n {\n namespace m {\n class C {}; \n} \n}";
+ if (argc > 1) {
+ QFile source(argv[1]);
+ if (!source.open(QFile::ReadOnly))
+ return 1;
+ defaultSource = source.readAll();
+ }
+ return clang::tooling::runToolOnCode(new FindNamedClassAction, defaultSource.data()) ? 0 : 2;
+}
+