diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c74fa5..042a89c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,7 @@ set(header_files Token.h Tokens.h TranslationUnit.h + Diagnostic.h ) set(cc_files CodeCompleteResults.cc diff --git a/src/Diagnostic.h b/src/Diagnostic.h new file mode 100644 index 0000000..75b58a9 --- /dev/null +++ b/src/Diagnostic.h @@ -0,0 +1,20 @@ +#ifndef DIAGNOSTICS_H_ +#define DIAGNOSTICS_H_ +#include "TranslationUnit.h" + +namespace clang { + class Diagnostic { + public: + class LocationData { + public: + unsigned line, column, offset; + }; + + unsigned severity; + std::string spelling; + std::string path; + LocationData start_location, end_location; + }; +} + +#endif // DIAGNOSTICS_H_ \ No newline at end of file diff --git a/src/SourceLocation.h b/src/SourceLocation.h index e6cd3b0..286ddbb 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -21,6 +21,8 @@ namespace clang { const std::string &filepath, int offset); + SourceLocation(CXSourceLocation location) {location_=location;} + explicit SourceLocation(Cursor *cursor); void get_location_info(std::string* path, diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index a91fa6d..71c535e 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -1,4 +1,6 @@ #include "TranslationUnit.h" +#include "SourceLocation.h" +#include "Tokens.h" clang::TranslationUnit:: ~TranslationUnit() { @@ -87,3 +89,36 @@ ReparseTranslationUnit(const std::string &file_path, unsigned clang::TranslationUnit::DefaultFlags() { return CXTranslationUnit_CacheCompletionResults | CXTranslationUnit_PrecompiledPreamble | CXTranslationUnit_Incomplete; } + +std::vector clang::TranslationUnit::get_diagnostics() { + std::vector diagnostics; + for(unsigned c=0;c #include #include "Index.h" +#include "Diagnostic.h" namespace clang { class Token; @@ -34,6 +35,8 @@ namespace clang { &buffers, unsigned flags=DefaultFlags()); static unsigned DefaultFlags(); + std::vector get_diagnostics(); + private: friend Token; friend Tokens; diff --git a/src/clangmm.h b/src/clangmm.h index d5b11c5..3ffe3ae 100644 --- a/src/clangmm.h +++ b/src/clangmm.h @@ -12,4 +12,5 @@ #include "CompletionString.h" #include "Index.h" #include "Cursor.h" +#include "Diagnostic.h" #endif // CLANGMM_H_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 01899af..de61c79 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ set(project_tests ${project_name}_tests) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_HOME_DIRECTORY}/cmake/Modules/") @@ -23,6 +23,7 @@ add_executable(${project_tests} Cursor_H_Test.cc Token_H_Test.cc SourceLocation_H_Test.cc + Diagnostics_Test.cc ) include_directories(${LIBCLANG_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} "${CMAKE_SOURCE_DIR}/src") diff --git a/tests/Diagnostics_Test.cc b/tests/Diagnostics_Test.cc new file mode 100644 index 0000000..f9e819d --- /dev/null +++ b/tests/Diagnostics_Test.cc @@ -0,0 +1,32 @@ +#include +#include "clangmm.h" +#include +#include + +using namespace std; + +BOOST_AUTO_TEST_CASE(diagnostics_test) { + std::string path("./case/main_error.cpp"); + + clang::Index index(0, 0); + + std::map map_buffers; + ifstream ifs(path, ifstream::in); + stringstream ss; + ss << ifs.rdbuf(); + + map_buffers["./case/main_error.cpp"]=ss.str(); + + std::vector args; + clang::TranslationUnit tu(&index, path, args, map_buffers); + + auto diagnostics=tu.get_diagnostics(); + BOOST_CHECK(diagnostics.size()==1); + BOOST_CHECK(diagnostics[0].spelling=="use of undeclared identifier 'undeclared_variable'"); + BOOST_CHECK(diagnostics[0].path=="./case/main_error.cpp"); + BOOST_CHECK(diagnostics[0].severity==3); + BOOST_CHECK(diagnostics[0].start_location.line==5); + BOOST_CHECK(diagnostics[0].end_location.line==5); + BOOST_CHECK(diagnostics[0].start_location.column==16); + BOOST_CHECK(diagnostics[0].end_location.column==35); +}