[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kdev-clang] tests: Add clang-standalone-parser for testing purposes
From: Kevin Funk <kevin () kfunk ! org>
Date: 2014-01-28 11:14:58
Message-ID: E1W86d8-0006DI-AN () scm ! kde ! org
[Download RAW message or body]
Git commit 1fa9eca7a9a58404c3ef687fcfa63800d459cff5 by Kevin Funk.
Committed on 28/01/2014 at 11:12.
Pushed by kfunk into branch 'master'.
Add clang-standalone-parser for testing purposes
M +7 -0 tests/CMakeLists.txt
A +117 -0 tests/clang-standalone-parser.c [License: GPL (v3+)]
http://commits.kde.org/kdev-clang/1fa9eca7a9a58404c3ef687fcfa63800d459cff5
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a1b854f..c51dc22 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -12,6 +12,13 @@ target_link_libraries(clang-parser
kdevclangduchain
)
+kde4_add_executable(clang-standalone-parser
+ clang-standalone-parser.c
+)
+target_link_libraries(clang-standalone-parser
+ ${CLANG_CLANG_LIB}
+)
+
########### next target ###############
kde4_add_unit_test(duchaintest
diff --git a/tests/clang-standalone-parser.c b/tests/clang-standalone-parser.c
new file mode 100644
index 0000000..dbad9e8
--- /dev/null
+++ b/tests/clang-standalone-parser.c
@@ -0,0 +1,117 @@
+/*
+ Copyright (C) 2011, 2012, 2013 Jan Erik Hanssen and Anders Bakken
+
+ 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 3 of the License, or
+ (at your option) any later version.
+
+ 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/>.
+
+ Source: https://github.com/Andersbakken/rtags/blob/master/src/clangtest.c
+*/
+
+#include <clang-c/Index.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static void printString(const char *name, CXString string)
+{
+ const char *cstr = clang_getCString(string);
+ if (cstr && *cstr) {
+ printf("%s: %s ", name, cstr);
+ }
+ clang_disposeString(string);
+}
+
+static void printCursor(CXCursor cursor)
+{
+ CXFile file;
+ unsigned off, line, col;
+ CXSourceLocation location = clang_getCursorLocation(cursor);
+ clang_getSpellingLocation(location, &file, &line, &col, &off);
+ CXString fileName = clang_getFileName(file);
+ const char *fileNameCStr = clang_getCString(fileName);
+ if (fileNameCStr) {
+ CXSourceRange range = clang_getCursorExtent(cursor);
+ unsigned start, end;
+ clang_getSpellingLocation(clang_getRangeStart(range), 0, 0, 0, &start);
+ clang_getSpellingLocation(clang_getRangeEnd(range), 0, 0, 0, &end);
+ printf("%s:%d:%d (%d, %d-%d) ", fileNameCStr, line, col, off, start, end);
+ }
+ clang_disposeString(fileName);
+ printString("kind", clang_getCursorKindSpelling(clang_getCursorKind(cursor)));
+ printString("type", clang_getTypeSpelling(clang_getCursorType(cursor)));
+ printString("display name", clang_getCursorDisplayName(cursor));
+ printString("usr", clang_getCursorUSR(cursor));
+ if (clang_isCursorDefinition(cursor))
+ printf("definition ");
+ printf("\n");
+}
+
+static enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent, CXClientData \
userData) +{
+ (void)parent;
+ int indent = *(int*)userData;
+ int i;
+ for (i=0; i<indent; ++i) {
+ printf(" ");
+ }
+ printCursor(cursor);
+ CXCursor ref = clang_getCursorReferenced(cursor);
+ if (!clang_isInvalid(clang_getCursorKind(ref)) && !clang_equalCursors(ref, \
cursor)) { + for (i=0; i<indent; ++i) {
+ printf(" ");
+ }
+ printf("-> ");
+ printCursor(ref);
+ }
+ ++indent;
+ clang_visitChildren(cursor, visit, &indent);
+ return CXChildVisit_Continue;
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 2)
+ return 1;
+ CXIndex index = clang_createIndex(1, 1);
+ const char * const *args = 0;
+ if (argc > 2)
+ args = (const char *const *)&argv[2];
+
+ CXTranslationUnit unit = clang_parseTranslationUnit(index, argv[1], args, argc - \
2, + 0, 0, \
clang_defaultEditingTranslationUnitOptions()); + if (unit) {
+ int indent = 0;
+ clang_visitChildren(clang_getTranslationUnitCursor(unit), visit, &indent);
+
+ const unsigned diagnosticCount = clang_getNumDiagnostics(unit);
+ unsigned i;
+ for (i=0; i<diagnosticCount; ++i) {
+ CXDiagnostic diagnostic = clang_getDiagnostic(unit, i);
+ const unsigned diagnosticOptions = (CXDiagnostic_DisplaySourceLocation|
+ CXDiagnostic_DisplayColumn|
+ CXDiagnostic_DisplaySourceRanges|
+ CXDiagnostic_DisplayOption|
+ CXDiagnostic_DisplayCategoryId|
+ CXDiagnostic_DisplayCategoryName);
+ CXString diagnosticText = clang_formatDiagnostic(diagnostic, \
diagnosticOptions); + const char *cstr = clang_getCString(diagnosticText);
+ if (cstr)
+ printf("%s\n", cstr);
+ clang_disposeString(diagnosticText);
+ }
+ clang_disposeTranslationUnit(unit);
+ }
+ clang_disposeIndex(index);
+ return 0;
+}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic