From e2c8256b26c2d9b3bb59f6d44351955d0b51f7e8 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 14 Jul 2015 23:31:30 +0200 Subject: [PATCH] Big cleanup, got rid of several circular includes and class forward declarations. Still some work left, but I'll wait with this. Main reason behind the cleanup was compilation troubles. --- src/CMakeLists.txt | 1 - src/CodeCompleteResults.cc | 4 +- src/CodeCompleteResults.h | 8 +- src/CompilationDatabase.h | 3 +- src/CompileCommand.h | 2 +- src/CompileCommands.h | 3 +- src/CompletionString.h | 8 +- src/Cursor.cc | 4 +- src/Cursor.h | 10 +-- src/Diagnostic.cc | 20 ++--- src/Diagnostic.h | 14 +--- src/Index.h | 4 - src/SourceLocation.cc | 16 ++-- src/SourceLocation.h | 16 ++-- src/SourceRange.cc | 22 ++++- src/SourceRange.h | 23 +++-- src/Token.cc | 16 ++-- src/Token.h | 19 +++-- src/Tokens.cc | 125 ++++++++++++++++++++++++++-- src/Tokens.h | 16 +++- src/TranslationUnit.cc | 2 +- src/TranslationUnit.h | 16 +--- tests/CodeCompleteResults_H_Test.cc | 2 +- tests/CompletionString_H_Test.cc | 2 +- tests/Cursor_H_Test.cc | 4 +- tests/Diagnostics_Test.cc | 6 +- tests/SourceLocation_H_Test.cc | 8 +- tests/Token_H_Test.cc | 8 +- 28 files changed, 243 insertions(+), 139 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1a1ae1c..b30f052 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,6 @@ message("Searcing for libclang") find_package(LibClang REQUIRED) set(header_files - clangmm.h CodeCompleteResults.h CompilationDatabase.h CompileCommand.h diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index 6a281e8..ea1b0f6 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -3,7 +3,7 @@ #include clang::CodeCompleteResults:: -CodeCompleteResults(clang::TranslationUnit *tu, +CodeCompleteResults(CXTranslationUnit &tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -16,7 +16,7 @@ CodeCompleteResults(clang::TranslationUnit *tu, file.Length = buffer.second.size(); files.push_back(file); } - results_ = clang_codeCompleteAt(tu->tu_, + results_ = clang_codeCompleteAt(tu, file_name.c_str(), line_num, column, diff --git a/src/CodeCompleteResults.h b/src/CodeCompleteResults.h index 8446a74..2269f68 100644 --- a/src/CodeCompleteResults.h +++ b/src/CodeCompleteResults.h @@ -1,14 +1,13 @@ #ifndef CODECOMPLETERESULTS_H_ #define CODECOMPLETERESULTS_H_ #include -#include "TranslationUnit.h" +#include +#include "CompletionString.h" namespace clang { - class CompletionString; - class CodeCompleteResults { public: - CodeCompleteResults(TranslationUnit *tu, + CodeCompleteResults(CXTranslationUnit &tu, const std::string &file_name, const std::map &buffers, int line_num, @@ -17,7 +16,6 @@ namespace clang { CompletionString get(int index); int size(); - private: CXCodeCompleteResults *results_; }; } // namespace clang diff --git a/src/CompilationDatabase.h b/src/CompilationDatabase.h index 8d66a57..7d35e56 100644 --- a/src/CompilationDatabase.h +++ b/src/CompilationDatabase.h @@ -11,9 +11,8 @@ namespace clang { explicit CompilationDatabase(const std::string &project_path); CompilationDatabase(); ~CompilationDatabase(); - private: + CXCompilationDatabase db_; - friend CompileCommands; }; } diff --git a/src/CompileCommand.h b/src/CompileCommand.h index f56dac8..cf3892e 100644 --- a/src/CompileCommand.h +++ b/src/CompileCommand.h @@ -9,7 +9,7 @@ namespace clang { CompileCommand(int nth, CompileCommands *commands); std::string get_command(); std::vector get_command_as_args(); - private: + CXCompileCommand command_; }; } diff --git a/src/CompileCommands.h b/src/CompileCommands.h index f56d859..7c4cb8d 100644 --- a/src/CompileCommands.h +++ b/src/CompileCommands.h @@ -12,9 +12,8 @@ namespace clang { CompileCommands(const std::string &filename, CompilationDatabase *db); std::vector get_commands(); ~CompileCommands(); - private: + CXCompileCommands commands_; - friend class CompileCommand; }; } #endif // COMPILECOMMANDS_H_ diff --git a/src/CompletionString.h b/src/CompletionString.h index eee7013..23f9ea2 100644 --- a/src/CompletionString.h +++ b/src/CompletionString.h @@ -1,7 +1,8 @@ #ifndef COMPLETIONSTRING_H_ #define COMPLETIONSTRING_H_ #include -#include "CodeCompleteResults.h" +#include +#include namespace clang { enum CompletionChunkKind { @@ -27,14 +28,13 @@ namespace clang { class CompletionString { public: + explicit CompletionString(const CXCompletionString &str); bool available(); std::vector get_chunks(); std::string get_brief_comments(); int get_num_chunks(); - private: - explicit CompletionString(const CXCompletionString &str); + CXCompletionString str_; - friend CodeCompleteResults; }; } // namespace clang #endif // COMPLETIONSTRING_H_ diff --git a/src/Cursor.cc b/src/Cursor.cc index e6ab0b7..7f9b3c8 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -5,6 +5,6 @@ const clang::CursorKind clang::Cursor::kind() { } clang::Cursor:: -Cursor(clang::TranslationUnit *tu, clang::SourceLocation *source_location) { - cursor_ = clang_getCursor(tu->tu_, source_location->location_); +Cursor(CXTranslationUnit &tu, clang::SourceLocation *source_location) { + cursor_ = clang_getCursor(tu, source_location->location_); } diff --git a/src/Cursor.h b/src/Cursor.h index 5740226..d7326ff 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -1,9 +1,11 @@ #ifndef CURSOR_H_ #define CURSOR_H_ -#include "TranslationUnit.h" +#include #include "SourceLocation.h" namespace clang { + class SourceLocation; + enum class CursorKind { UnexposedDecl = 1, StructDecl = 2, @@ -175,13 +177,9 @@ namespace clang { class Cursor { public: Cursor() {} - Cursor(TranslationUnit *tu, SourceLocation *source_location); + Cursor(CXTranslationUnit &tu, SourceLocation *source_location); const CursorKind kind(); - private: CXCursor cursor_; - friend SourceRange; - friend SourceLocation; - friend Tokens; }; } // namespace clang #endif // CURSOR_H_ diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index 5712527..fa446e7 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -2,29 +2,19 @@ #include "SourceLocation.h" #include "Tokens.h" -clang::Diagnostic::Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic) { +clang::Diagnostic::Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic) { severity=clang_getDiagnosticSeverity(clang_diagnostic); severity_spelling=get_severity_spelling(severity); spelling=clang_getCString(clang_getDiagnosticSpelling(clang_diagnostic)); clang::SourceLocation location(clang_getDiagnosticLocation(clang_diagnostic)); - std::string tmp_path; - unsigned line, column, offset; - location.get_location_info(&tmp_path, &line, &column, &offset); - path=tmp_path; - start_location.line=line; - start_location.column=column; - start_location.offset=offset; clang::SourceRange range(&location, &location); - clang::Tokens tokens(&tu, &range); + clang::Tokens tokens(tu, &range); if(tokens.size()==1) { auto& token=tokens[0]; - clang::SourceRange range=token.get_source_range(&tu); - clang::SourceLocation location(&range, false); - location.get_location_info(NULL, &line, &column, &offset); - end_location.line=line; - end_location.column=column; - end_location.offset=offset; + clang::SourceRange range=token.get_source_range(); + auto end_location=clang::SourceLocation(&range, false); + this->range=range.get_range_data(location, end_location); } } diff --git a/src/Diagnostic.h b/src/Diagnostic.h index c858d33..773b986 100644 --- a/src/Diagnostic.h +++ b/src/Diagnostic.h @@ -3,25 +3,19 @@ #include #include #include -#include "TranslationUnit.h" +#include "SourceRange.h" -namespace clang { +namespace clang { class Diagnostic { public: - class LocationData { - public: - unsigned line, column, offset; - }; - - Diagnostic(clang::TranslationUnit& tu, CXDiagnostic& clang_diagnostic); + Diagnostic(CXTranslationUnit& tu, CXDiagnostic& clang_diagnostic); static const std::string get_severity_spelling(unsigned severity); unsigned severity; std::string severity_spelling; std::string spelling; - std::string path; - LocationData start_location, end_location; + RangeData range; }; } diff --git a/src/Index.h b/src/Index.h index f93feab..6e71e7b 100644 --- a/src/Index.h +++ b/src/Index.h @@ -1,16 +1,12 @@ #ifndef INDEX_H_ #define INDEX_H_ - #include namespace clang { - class TranslationUnit; class Index { public: Index(int excludeDeclarationsFromPCH, int displayDiagnostics); - private: CXIndex index_; - friend TranslationUnit; }; } // namespace clang #endif // INDEX_H_ diff --git a/src/SourceLocation.cc b/src/SourceLocation.cc index c68d32f..13506ae 100644 --- a/src/SourceLocation.cc +++ b/src/SourceLocation.cc @@ -4,13 +4,13 @@ // SourceLocation // // // // // // // // // clang::SourceLocation:: -SourceLocation(clang::TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, const std::string &filename, int line_number, int line_offset) { - CXFile file = clang_getFile(tu->tu_, + CXFile file = clang_getFile(tu, filename.c_str()); - location_ = clang_getLocation(tu->tu_, + location_ = clang_getLocation(tu, file, line_number, line_offset); @@ -28,19 +28,19 @@ SourceLocation(clang::SourceRange *range, bool start) { } clang::SourceLocation:: -SourceLocation(TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, Token *token) { - location_ = clang_getTokenLocation(tu->tu_, + location_ = clang_getTokenLocation(tu, token->token_); } clang::SourceLocation:: -SourceLocation(clang::TranslationUnit *tu, +SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset) { - CXFile file = clang_getFile(tu->tu_, + CXFile file = clang_getFile(tu, filepath.c_str()); - location_ = clang_getLocationForOffset(tu->tu_, + location_ = clang_getLocationForOffset(tu, file, offset); } diff --git a/src/SourceLocation.h b/src/SourceLocation.h index 286ddbb..81394ec 100644 --- a/src/SourceLocation.h +++ b/src/SourceLocation.h @@ -1,23 +1,28 @@ #ifndef SOURCELOCATION_H_ #define SOURCELOCATION_H_ -#include "TranslationUnit.h" +#include #include "Token.h" #include "Cursor.h" +#include namespace clang { + class Token; + class SourceRange; + class Cursor; + class SourceLocation { public: - SourceLocation(TranslationUnit* tu, + SourceLocation(CXTranslationUnit &tu, const std::string &filename, int line_number, int column); - SourceLocation(TranslationUnit *tu, + SourceLocation(CXTranslationUnit &tu, Token *token); SourceLocation(SourceRange *range, bool start); - SourceLocation(TranslationUnit *tu, + SourceLocation(CXTranslationUnit &tu, const std::string &filepath, int offset); @@ -30,10 +35,7 @@ namespace clang { unsigned *column, unsigned *offset); - private: CXSourceLocation location_; - friend SourceRange; - friend Cursor; }; } // namespace clang diff --git a/src/SourceRange.cc b/src/SourceRange.cc index 87581d4..8de63a6 100644 --- a/src/SourceRange.cc +++ b/src/SourceRange.cc @@ -1,8 +1,8 @@ #include "SourceRange.h" clang::SourceRange:: -SourceRange(clang::TranslationUnit *tu, clang::Token *token) { - range_ = clang_getTokenExtent(tu->tu_, token->token_); +SourceRange(clang::Token *token) { + range_ = clang_getTokenExtent(token->tu, token->token_); } clang::SourceRange:: @@ -13,3 +13,21 @@ SourceRange(clang::SourceLocation *start, clang::SourceLocation *end) { clang::SourceRange::SourceRange(Cursor *cursor) { range_ = clang_getCursorExtent(cursor->cursor_); } + +clang::RangeData clang::SourceRange::get_range_data(clang::SourceLocation &start, clang::SourceLocation &end) { + std::string path; + unsigned start_offset, end_offset; + start.get_location_info(&path, NULL, NULL, &start_offset); + end.get_location_info(NULL, NULL, NULL, &end_offset); + RangeData range_data; + range_data.path=path; + range_data.start_offset=start_offset; + range_data.end_offset=end_offset; + return range_data; +} + +clang::RangeData clang::SourceRange::get_range_data() { + clang::SourceLocation start(this, true); + clang::SourceLocation end(this, false); + return get_range_data(start, end); +} \ No newline at end of file diff --git a/src/SourceRange.h b/src/SourceRange.h index d1f772b..be74888 100644 --- a/src/SourceRange.h +++ b/src/SourceRange.h @@ -1,23 +1,32 @@ #ifndef SOURCERANGE_H_ #define SOURCERANGE_H_ -#include "TranslationUnit.h" +#include #include "Token.h" #include "Cursor.h" +#include "SourceLocation.h" +#include namespace clang { + class Token; + class SourceLocation; + class Cursor; + + class RangeData { + public: + std::string path; + unsigned start_offset, end_offset; + }; + class SourceRange { public: SourceRange() {} - SourceRange(TranslationUnit *tu, Token *token); + SourceRange(Token *token); SourceRange(SourceLocation *start, SourceLocation *end); explicit SourceRange(Cursor *cursor); - - private: + static RangeData get_range_data(SourceLocation &start, SourceLocation &end); + RangeData get_range_data(); CXSourceRange range_; - friend Tokens; - friend SourceLocation; - friend Diagnostic; }; } // namespace clang #endif // SOURCERANGE_H_ diff --git a/src/Token.cc b/src/Token.cc index 455da1a..6ac0945 100644 --- a/src/Token.cc +++ b/src/Token.cc @@ -5,25 +5,23 @@ // // // // // // clang::Token instansiates an token -clang::Token::Token(const CXToken &token) : - token_(token) { +clang::Token::Token(CXTranslationUnit &tu, const CXToken &token) : + tu(tu), token_(token) { } // returns gets an source location for this token objekt // based on the translationunit given -clang::SourceLocation clang::Token:: - get_source_location(clang::TranslationUnit *tu) { +clang::SourceLocation clang::Token::get_source_location() { return SourceLocation(tu, this); } // returns a sourcerange that covers this token -clang::SourceRange clang::Token:: - get_source_range(clang::TranslationUnit *tu) { - return SourceRange(tu, this); +clang::SourceRange clang::Token::get_source_range() { + return SourceRange(this); } // returns a string description of this tokens kind -std::string clang::Token::get_token_spelling(clang::TranslationUnit *tu) { - CXString s = clang_getTokenSpelling(tu->tu_, token_); +std::string clang::Token::get_token_spelling() { + CXString s = clang_getTokenSpelling(tu, token_); return std::string(clang_getCString(s)); } diff --git a/src/Token.h b/src/Token.h index dd8efe4..627b6ef 100644 --- a/src/Token.h +++ b/src/Token.h @@ -1,10 +1,14 @@ #ifndef TOKEN_H_ #define TOKEN_H_ +#include #include "SourceLocation.h" #include "SourceRange.h" -#include "TranslationUnit.h" +#include namespace clang { + class SourceLocation; + class SourceRange; + enum TokenKind { Token_Punctuation, Token_Keyword, @@ -15,17 +19,14 @@ namespace clang { class Token { public: + explicit Token(CXTranslationUnit &tu, const CXToken &token); const TokenKind kind(); - std::string get_token_spelling(TranslationUnit *tu); - SourceLocation get_source_location(TranslationUnit *tu); - SourceRange get_source_range(TranslationUnit *tu); + std::string get_token_spelling(); + SourceLocation get_source_location(); + SourceRange get_source_range(); std::string type; - private: - explicit Token(const CXToken &token); - friend SourceRange; - friend SourceLocation; - friend Tokens; const CXToken& token_; + CXTranslationUnit &tu; }; } // namespace clang #endif // TOKEN_H_ diff --git a/src/Tokens.cc b/src/Tokens.cc index 571d0db..9b2b1e8 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -2,24 +2,24 @@ #include using namespace std; -clang::Tokens::Tokens(clang::TranslationUnit *tu, clang::SourceRange *range): tu(*tu) { - clang_tokenize(tu->tu_, +clang::Tokens::Tokens(CXTranslationUnit &tu, clang::SourceRange *range): tu(tu) { + clang_tokenize(tu, range->range_, &tokens_, &num_tokens_); for (int i = 0; i < num_tokens_; i++) { - push_back(clang::Token(tokens_[i])); + emplace_back(tu, tokens_[i]); } } clang::Tokens::~Tokens() { - clang_disposeTokens(tu.tu_, tokens_, size()); + clang_disposeTokens(tu, tokens_, size()); } -void clang::Tokens::update_types(clang::TranslationUnit *tu) { +void clang::Tokens::update_types() { clang_cursors.clear(); clang_cursors.reserve(size()); - clang_annotateTokens(tu->tu_, tokens_, size(), clang_cursors.data()); + clang_annotateTokens(tu, tokens_, size(), clang_cursors.data()); for(size_t c=0;c > &ranges) { + for(size_t c=0;c #include "SourceRange.h" #include "Token.h" +#include +#include namespace clang { class Tokens : public std::vector { public: - Tokens(TranslationUnit *tu, SourceRange *range); + Tokens(CXTranslationUnit &tu, SourceRange *range); ~Tokens(); - void update_types(clang::TranslationUnit *tu); + void update_types(); std::string get_brief_comments(size_t cursor_id); + CXCursor find_referenced(); + bool equalCursors(CXCursor a, CXCursor b); + void rename(CXCursor &referenced, std::unordered_map > &ranges); + private: CXToken *tokens_; unsigned num_tokens_; std::vector clang_cursors; - TranslationUnit& tu; + CXTranslationUnit& tu; + + static CXChildVisitResult clang_visitor(CXCursor cursor, CXCursor parent, CXClientData clientData); }; } // namespace clang #endif // TOKENS_H_ diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index de1c2aa..497c62d 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -101,7 +101,7 @@ void clang::TranslationUnit::update_diagnostics() { diagnostics.clear(); for(unsigned c=0;c diagnostics; - private: + void parse(Index *index, const std::string &filepath, const std::vector &command_line_args, const std::map &buffers, unsigned flags=DefaultFlags()); - friend Token; - friend Tokens; - friend SourceLocation; - friend SourceRange; - friend Cursor; - friend CodeCompleteResults; CXTranslationUnit tu_; }; } // namespace clang diff --git a/tests/CodeCompleteResults_H_Test.cc b/tests/CodeCompleteResults_H_Test.cc index c696727..a5b4ad1 100644 --- a/tests/CodeCompleteResults_H_Test.cc +++ b/tests/CodeCompleteResults_H_Test.cc @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(code_complete_results) { // ] - clang::CodeCompleteResults results(&tu, path, buffers, 4, 5); + clang::CodeCompleteResults results(tu.tu_, path, buffers, 4, 5); bool substr_found=false; for(int c=0;c