From ca2021d93599de931ba5219efd36b0ecb930e3b5 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 6 Jul 2016 11:07:37 +0200 Subject: [PATCH] Cleanup, added Cursor::Type class, and extra functions to Cursor --- src/CMakeLists.txt | 4 +- src/CodeCompleteResults.cc | 4 +- src/CompilationDatabase.cc | 6 +- src/CompilationDatabase.h | 1 - src/CompileCommand.cc | 10 +- src/CompileCommands.cc | 11 +- src/CompletionString.cc | 9 +- src/Cursor.cc | 54 ++++-- src/Cursor.h | 358 +++++++++++++++++++------------------ src/Diagnostic.cc | 10 +- src/Index.cc | 3 +- src/SourceLocation.cc | 2 +- src/SourceRange.cc | 3 +- src/Token.cc | 6 +- src/Token.h | 19 +- src/Tokens.cc | 24 +-- src/Tokens.h | 2 +- src/TranslationUnit.cc | 26 +-- tests/Cursor_H_Test.cc | 2 +- tests/Token_H_Test.cc | 2 +- 20 files changed, 296 insertions(+), 260 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2812e6c..75506fc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,7 @@ set(header_files CompileCommand.h CompileCommands.h CompletionString.h - Cursor.h + Cursor.h Index.h SourceLocation.h SourceRange.h @@ -34,7 +34,7 @@ set(cc_files CompileCommand.cc CompileCommands.cc CompletionString.cc - Cursor.cc + Cursor.cc Index.cc SourceLocation.cc SourceRange.cc diff --git a/src/CodeCompleteResults.cc b/src/CodeCompleteResults.cc index 38c90fd..a555243 100644 --- a/src/CodeCompleteResults.cc +++ b/src/CodeCompleteResults.cc @@ -7,7 +7,7 @@ clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer, unsigned line_num, unsigned column) { CXUnsavedFile files[1]; - auto file_path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); + auto file_path=to_string(clang_getTranslationUnitSpelling(cx_tu)); files[0].Filename = file_path.c_str(); files[0].Contents = buffer.c_str(); files[0].Length = buffer.size(); @@ -41,5 +41,5 @@ clang::CompletionString clang::CodeCompleteResults::get(unsigned i) const { } std::string clang::CodeCompleteResults::get_usr() const { - return clang::to_string(clang_codeCompleteGetContainerUSR(cx_results)); + return to_string(clang_codeCompleteGetContainerUSR(cx_results)); } diff --git a/src/CompilationDatabase.cc b/src/CompilationDatabase.cc index cdcc099..46b2c83 100644 --- a/src/CompilationDatabase.cc +++ b/src/CompilationDatabase.cc @@ -1,8 +1,7 @@ #include "CompilationDatabase.h" #include -clang::CompilationDatabase:: -CompilationDatabase(const std::string &project_path) { +clang::CompilationDatabase::CompilationDatabase(const std::string &project_path) { CXCompilationDatabase_Error error; cx_db = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error); if(error) { @@ -10,7 +9,6 @@ CompilationDatabase(const std::string &project_path) { } } -clang::CompilationDatabase:: -~CompilationDatabase() { +clang::CompilationDatabase::~CompilationDatabase() { clang_CompilationDatabase_dispose(cx_db); } diff --git a/src/CompilationDatabase.h b/src/CompilationDatabase.h index 0527fdf..2e4e81a 100644 --- a/src/CompilationDatabase.h +++ b/src/CompilationDatabase.h @@ -8,7 +8,6 @@ namespace clang { class CompilationDatabase { public: explicit CompilationDatabase(const std::string &project_path); - CompilationDatabase(); ~CompilationDatabase(); CXCompilationDatabase cx_db; diff --git a/src/CompileCommand.cc b/src/CompileCommand.cc index 5c4ec93..a291da5 100644 --- a/src/CompileCommand.cc +++ b/src/CompileCommand.cc @@ -2,22 +2,20 @@ #include "CompileCommands.h" #include "Utility.h" -std::string clang::CompileCommand:: -get_command() { +std::string clang::CompileCommand::get_command() { std::string res; unsigned N = clang_CompileCommand_getNumArgs(cx_command); for (unsigned i = 0; i < N; i++) { - res += clang::to_string(clang_CompileCommand_getArg(cx_command, i)); + res += to_string(clang_CompileCommand_getArg(cx_command, i)); } return res; } -std::vector clang::CompileCommand:: -get_command_as_args() { +std::vector clang::CompileCommand::get_command_as_args() { unsigned N = clang_CompileCommand_getNumArgs(cx_command); std::vector res(N); for (unsigned i = 0; i < N; i++) { - res[i] = clang::to_string(clang_CompileCommand_getArg(cx_command, i)); + res[i] = to_string(clang_CompileCommand_getArg(cx_command, i)); } return res; } diff --git a/src/CompileCommands.cc b/src/CompileCommands.cc index 2b5494b..44fddcb 100644 --- a/src/CompileCommands.cc +++ b/src/CompileCommands.cc @@ -1,22 +1,19 @@ #include "CompileCommands.h" -clang::CompileCommands:: -CompileCommands(const std::string &filename, CompilationDatabase &db) { +clang::CompileCommands::CompileCommands(const std::string &filename, CompilationDatabase &db) { cx_commands = clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str()); if(clang_CompileCommands_getSize(cx_commands)==0) cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db); } -clang::CompileCommands:: -~CompileCommands() { +clang::CompileCommands::~CompileCommands() { clang_CompileCommands_dispose(cx_commands); } -std::vector clang::CompileCommands:: -get_commands() { +std::vector clang::CompileCommands::get_commands() { unsigned N = clang_CompileCommands_getSize(cx_commands); - std::vector res; + std::vector res; for (unsigned i = 0; i < N; i++) { res.emplace_back(clang_CompileCommands_getCommand(cx_commands, i)); } diff --git a/src/CompletionString.cc b/src/CompletionString.cc index 9833466..e9b465e 100644 --- a/src/CompletionString.cc +++ b/src/CompletionString.cc @@ -13,17 +13,16 @@ unsigned clang::CompletionString::get_num_chunks() { } std::vector clang::CompletionString::get_chunks() { - std::vector res; + std::vector res; for (unsigned i = 0; i < get_num_chunks(); i++) { - res.emplace_back(clang::to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast (clang_getCompletionChunkKind(cx_completion_sting, i))); + res.emplace_back(to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast (clang_getCompletionChunkKind(cx_completion_sting, i))); } return res; } std::string clang::CompletionString::get_brief_comments() { - return clang::to_string(clang_getCompletionBriefComment(cx_completion_sting)); + return to_string(clang_getCompletionBriefComment(cx_completion_sting)); } -clang::CompletionChunk:: -CompletionChunk(std::string chunk, clang::CompletionChunkKind kind) : +clang::CompletionChunk::CompletionChunk(std::string chunk, CompletionChunkKind kind) : chunk(chunk), kind(kind) { } diff --git a/src/Cursor.cc b/src/Cursor.cc index f413c45..7b61124 100644 --- a/src/Cursor.cc +++ b/src/Cursor.cc @@ -2,8 +2,24 @@ #include "Utility.h" #include -clang::CursorKind clang::Cursor::get_kind() { - return static_cast(clang_getCursorKind(this->cx_cursor)); +std::string clang::Cursor::Type::get_spelling() const { + return to_string(clang_getTypeSpelling(cx_type)); +} + +clang::Cursor::Type clang::Cursor::Type::get_result() const { + return Type(clang_getResultType(cx_type)); +} + +bool clang::Cursor::Type::operator==(const Cursor::Type& rhs) const { + return clang_equalTypes(cx_type, rhs.cx_type); +} + +clang::Cursor::Kind clang::Cursor::get_kind() const { + return static_cast(clang_getCursorKind(cx_cursor)); +} + +clang::Cursor::Type clang::Cursor::get_type() const { + return Type(clang_getCursorType(cx_cursor)); } clang::SourceLocation clang::Cursor::get_source_location() const { @@ -15,19 +31,35 @@ clang::SourceRange clang::Cursor::get_source_range() const { } std::string clang::Cursor::get_spelling() const { - return clang::to_string(clang_getCursorSpelling(cx_cursor)); + return to_string(clang_getCursorSpelling(cx_cursor)); } std::string clang::Cursor::get_usr() const { - return clang::to_string(clang_getCursorUSR(cx_cursor)); + return to_string(clang_getCursorUSR(cx_cursor)); } clang::Cursor clang::Cursor::get_referenced() const { return Cursor(clang_getCursorReferenced(cx_cursor)); } +clang::Cursor clang::Cursor::get_canonical() const { + return Cursor(clang_getCanonicalCursor(cx_cursor)); +} + +clang::Cursor clang::Cursor::get_definition() const { + return Cursor(clang_getCursorDefinition(cx_cursor)); +} + clang::Cursor clang::Cursor::get_semantic_parent() const { - return clang::Cursor(clang_getCursorSemanticParent(cx_cursor)); + return Cursor(clang_getCursorSemanticParent(cx_cursor)); +} + +std::vector clang::Cursor::get_arguments() const { + std::vector cursors; + auto size=clang_Cursor_getNumArguments(cx_cursor); + for(int c=0;c=4 && std::equal(auto_str.begin(), auto_str.end(), spelling.begin())) { auto canonical_type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); - auto canonical_spelling=clang::to_string(clang_getTypeSpelling(canonical_type)); + auto canonical_spelling=to_string(clang_getTypeSpelling(canonical_type)); if(spelling.size()>5 && spelling[4]==' ' && spelling[5]=='&' && spelling!=canonical_spelling) return canonical_spelling+" &"; else @@ -67,7 +99,7 @@ std::string clang::Cursor::get_type() { const std::string const_auto_str="const auto"; if(spelling.size()>=10 && std::equal(const_auto_str.begin(), const_auto_str.end(), spelling.begin())) { auto canonical_type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); - auto canonical_spelling=clang::to_string(clang_getTypeSpelling(canonical_type)); + auto canonical_spelling=to_string(clang_getTypeSpelling(canonical_type)); if(spelling.size()>11 && spelling[10]==' ' && spelling[11]=='&' && spelling!=canonical_spelling) return canonical_spelling+" &"; else @@ -83,7 +115,7 @@ std::string clang::Cursor::get_brief_comments() { std::string comment_string; auto referenced=get_referenced(); if(referenced) { - comment_string=clang::to_string(clang_Cursor_getBriefCommentText(referenced.cx_cursor)); + comment_string=to_string(clang_Cursor_getBriefCommentText(referenced.cx_cursor)); } return comment_string; } diff --git a/src/Cursor.h b/src/Cursor.h index bf10fcf..083f747 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -4,192 +4,206 @@ #include "SourceLocation.h" #include "SourceRange.h" #include +#include -namespace clang { - enum class CursorKind { - UnexposedDecl = 1, - StructDecl = 2, - UnionDecl = 3, - ClassDecl = 4, - EnumDecl = 5, - FieldDecl = 6, - EnumConstantDecl = 7, - FunctionDecl = 8, - VarDecl = 9, - ParmDecl = 10, - ObjCInterfaceDecl = 11, - ObjCCategoryDecl = 12, - ObjCProtocolDecl = 13, - ObjCPropertyDecl = 14, - ObjCIvarDecl = 15, - ObjCInstanceMethodDecl = 16, - ObjCClassMethodDecl = 17, - ObjCImplementationDecl = 18, - ObjCCategoryImplDecl = 19, - TypedefDecl = 20, - CXXMethod = 21, - Namespace = 22, - LinkageSpec = 23, - Constructor = 24, - Destructor = 25, - ConversionFunction = 26, - TemplateTypeParameter = 27, - NonTypeTemplateParameter = 28, - TemplateTemplateParameter = 29, - FunctionTemplate = 30, - ClassTemplate = 31, - ClassTemplatePartialSpecialization = 32, - NamespaceAlias = 33, - UsingDirective = 34, - UsingDeclaration = 35, - TypeAliasDecl = 36, - ObjCSynthesizeDecl = 37, - ObjCDynamicDecl = 38, - CXXAccessSpecifier = 39, - FirstDecl = UnexposedDecl, - LastDecl = CXXAccessSpecifier, - FirstRef = 40, - ObjCSuperClassRef = 40, - ObjCProtocolRef = 41, - ObjCClassRef = 42, - TypeRef = 43, - CXXBaseSpecifier = 44, - TemplateRef = 45, - NamespaceRef = 46, - MemberRef = 47, - LabelRef = 48, - OverloadedDeclRef = 49, - VariableRef = 50, - LastRef = VariableRef, - FirstInvalid = 70, - InvalidFile = 70, - NoDeclFound = 71, - NotImplemented = 72, - InvalidCode = 73, - LastInvalid = InvalidCode, - FirstExpr = 100, - UnexposedExpr = 100, - DeclRefExpr = 101, - MemberRefExpr = 102, - CallExpr = 103, - ObjCMessageExpr = 104, - BlockExpr = 105, - IntegerLiteral = 106, - FloatingLiteral = 107, - ImaginaryLiteral = 108, - StringLiteral = 109, - CharacterLiteral = 110, - ParenExpr = 111, - UnaryOperator = 112, - ArraySubscriptExpr = 113, - BinaryOperator = 114, - CompoundAssignOperator = 115, - ConditionalOperator = 116, - CStyleCastExpr = 117, - CompoundLiteralExpr = 118, - InitListExpr = 119, - AddrLabelExpr = 120, - StmtExpr = 121, - GenericSelectionExpr = 122, - GNUNullExpr = 123, - CXXStaticCastExpr = 124, - CXXDynamicCastExpr = 125, - CXXReinterpretCastExpr = 126, - CXXConstCastExpr = 127, - CXXFunctionalCastExpr = 128, - CXXTypeidExpr = 129, - CXXBoolLiteralExpr = 130, - CXXNullPtrLiteralExpr = 131, - CXXThisExpr = 132, - CXXThrowExpr = 133, - CXXNewExpr = 134, - CXXDeleteExpr = 135, - UnaryExpr = 136, - ObjCStringLiteral = 137, - ObjCEncodeExpr = 138, - ObjCSelectorExpr = 139, - ObjCProtocolExpr = 140, - ObjCBridgedCastExpr = 141, - PackExpansionExpr = 142, - SizeOfPackExpr = 143, - LambdaExpr = 144, - ObjCBoolLiteralExpr = 145, - ObjCSelfExpr = 146, - LastExpr = ObjCSelfExpr, - FirstStmt = 200, - UnexposedStmt = 200, - LabelStmt = 201, - CompoundStmt = 202, - CaseStmt = 203, - DefaultStmt = 204, - IfStmt = 205, - SwitchStmt = 206, - WhileStmt = 207, - DoStmt = 208, - ForStmt = 209, - GotoStmt = 210, - IndirectGotoStmt = 211, - ContinueStmt = 212, - BreakStmt = 213, - ReturnStmt = 214, - GCCAsmStmt = 215, - AsmStmt = GCCAsmStmt, - ObjCAtTryStmt = 216, - ObjCAtCatchStmt = 217, - ObjCAtFinallyStmt = 218, - ObjCAtThrowStmt = 219, - ObjCAtSynchronizedStmt = 220, - ObjCAutoreleasePoolStmt = 221, - ObjCForCollectionStmt = 222, - CXXCatchStmt = 223, - CXXTryStmt = 224, - CXXForRangeStmt = 225, - SEHTryStmt = 226, - SEHExceptStmt = 227, - SEHFinallyStmt = 228, - MSAsmStmt = 229, - NullStmt = 230, - DeclStmt = 231, - LastStmt = DeclStmt, - TranslationUnit = 300, - FirstAttr = 400, - UnexposedAttr = 400, - IBActionAttr = 401, - IBOutletAttr = 402, - IBOutletCollectionAttr = 403, - CXXFinalAttr = 404, - CXXOverrideAttr = 405, - AnnotateAttr = 406, - AsmLabelAttr = 407, - LastAttr = AsmLabelAttr, - PreprocessingDirective = 500, - MacroDefinition = 501, - MacroExpansion = 502, - MacroInstantiation = MacroExpansion, - InclusionDirective = 503, - FirstPreprocessing = PreprocessingDirective, - LastPreprocessing = InclusionDirective, - ModuleImportDecl = 600, - FirstExtraDecl = ModuleImportDecl, - LastExtraDecl = ModuleImportDecl, - }; - +namespace clang { class Cursor { public: + enum class Kind { + UnexposedDecl = 1, + StructDecl = 2, + UnionDecl = 3, + ClassDecl = 4, + EnumDecl = 5, + FieldDecl = 6, + EnumConstantDecl = 7, + FunctionDecl = 8, + VarDecl = 9, + ParmDecl = 10, + ObjCInterfaceDecl = 11, + ObjCCategoryDecl = 12, + ObjCProtocolDecl = 13, + ObjCPropertyDecl = 14, + ObjCIvarDecl = 15, + ObjCInstanceMethodDecl = 16, + ObjCClassMethodDecl = 17, + ObjCImplementationDecl = 18, + ObjCCategoryImplDecl = 19, + TypedefDecl = 20, + CXXMethod = 21, + Namespace = 22, + LinkageSpec = 23, + Constructor = 24, + Destructor = 25, + ConversionFunction = 26, + TemplateTypeParameter = 27, + NonTypeTemplateParameter = 28, + TemplateTemplateParameter = 29, + FunctionTemplate = 30, + ClassTemplate = 31, + ClassTemplatePartialSpecialization = 32, + NamespaceAlias = 33, + UsingDirective = 34, + UsingDeclaration = 35, + TypeAliasDecl = 36, + ObjCSynthesizeDecl = 37, + ObjCDynamicDecl = 38, + CXXAccessSpecifier = 39, + FirstDecl = UnexposedDecl, + LastDecl = CXXAccessSpecifier, + FirstRef = 40, + ObjCSuperClassRef = 40, + ObjCProtocolRef = 41, + ObjCClassRef = 42, + TypeRef = 43, + CXXBaseSpecifier = 44, + TemplateRef = 45, + NamespaceRef = 46, + MemberRef = 47, + LabelRef = 48, + OverloadedDeclRef = 49, + VariableRef = 50, + LastRef = VariableRef, + FirstInvalid = 70, + InvalidFile = 70, + NoDeclFound = 71, + NotImplemented = 72, + InvalidCode = 73, + LastInvalid = InvalidCode, + FirstExpr = 100, + UnexposedExpr = 100, + DeclRefExpr = 101, + MemberRefExpr = 102, + CallExpr = 103, + ObjCMessageExpr = 104, + BlockExpr = 105, + IntegerLiteral = 106, + FloatingLiteral = 107, + ImaginaryLiteral = 108, + StringLiteral = 109, + CharacterLiteral = 110, + ParenExpr = 111, + UnaryOperator = 112, + ArraySubscriptExpr = 113, + BinaryOperator = 114, + CompoundAssignOperator = 115, + ConditionalOperator = 116, + CStyleCastExpr = 117, + CompoundLiteralExpr = 118, + InitListExpr = 119, + AddrLabelExpr = 120, + StmtExpr = 121, + GenericSelectionExpr = 122, + GNUNullExpr = 123, + CXXStaticCastExpr = 124, + CXXDynamicCastExpr = 125, + CXXReinterpretCastExpr = 126, + CXXConstCastExpr = 127, + CXXFunctionalCastExpr = 128, + CXXTypeidExpr = 129, + CXXBoolLiteralExpr = 130, + CXXNullPtrLiteralExpr = 131, + CXXThisExpr = 132, + CXXThrowExpr = 133, + CXXNewExpr = 134, + CXXDeleteExpr = 135, + UnaryExpr = 136, + ObjCStringLiteral = 137, + ObjCEncodeExpr = 138, + ObjCSelectorExpr = 139, + ObjCProtocolExpr = 140, + ObjCBridgedCastExpr = 141, + PackExpansionExpr = 142, + SizeOfPackExpr = 143, + LambdaExpr = 144, + ObjCBoolLiteralExpr = 145, + ObjCSelfExpr = 146, + LastExpr = ObjCSelfExpr, + FirstStmt = 200, + UnexposedStmt = 200, + LabelStmt = 201, + CompoundStmt = 202, + CaseStmt = 203, + DefaultStmt = 204, + IfStmt = 205, + SwitchStmt = 206, + WhileStmt = 207, + DoStmt = 208, + ForStmt = 209, + GotoStmt = 210, + IndirectGotoStmt = 211, + ContinueStmt = 212, + BreakStmt = 213, + ReturnStmt = 214, + GCCAsmStmt = 215, + AsmStmt = GCCAsmStmt, + ObjCAtTryStmt = 216, + ObjCAtCatchStmt = 217, + ObjCAtFinallyStmt = 218, + ObjCAtThrowStmt = 219, + ObjCAtSynchronizedStmt = 220, + ObjCAutoreleasePoolStmt = 221, + ObjCForCollectionStmt = 222, + CXXCatchStmt = 223, + CXXTryStmt = 224, + CXXForRangeStmt = 225, + SEHTryStmt = 226, + SEHExceptStmt = 227, + SEHFinallyStmt = 228, + MSAsmStmt = 229, + NullStmt = 230, + DeclStmt = 231, + LastStmt = DeclStmt, + TranslationUnit = 300, + FirstAttr = 400, + UnexposedAttr = 400, + IBActionAttr = 401, + IBOutletAttr = 402, + IBOutletCollectionAttr = 403, + CXXFinalAttr = 404, + CXXOverrideAttr = 405, + AnnotateAttr = 406, + AsmLabelAttr = 407, + LastAttr = AsmLabelAttr, + PreprocessingDirective = 500, + MacroDefinition = 501, + MacroExpansion = 502, + MacroInstantiation = MacroExpansion, + InclusionDirective = 503, + FirstPreprocessing = PreprocessingDirective, + LastPreprocessing = InclusionDirective, + ModuleImportDecl = 600, + FirstExtraDecl = ModuleImportDecl, + LastExtraDecl = ModuleImportDecl, + }; + class Type { + public: + Type(const CXType &cx_type) : cx_type(cx_type) {} + std::string get_spelling() const; + Type get_result() const; + bool operator==(const Cursor::Type& rhs) const; + + CXType cx_type; + }; + Cursor() { cx_cursor=clang_getNullCursor(); } Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {} - CursorKind get_kind(); + Kind get_kind() const; + Type get_type() const; SourceLocation get_source_location() const; SourceRange get_source_range() const; std::string get_spelling() const; std::string get_usr() const; Cursor get_referenced() const; + Cursor get_canonical() const; + Cursor get_definition() const; Cursor get_semantic_parent() const; + std::vector get_arguments() const; operator bool() const; bool operator==(const Cursor& rhs) const; - bool has_type(); - std::string get_type(); + bool has_type_description(); + std::string get_type_description(); std::string get_brief_comments(); CXCursor cx_cursor; diff --git a/src/Diagnostic.cc b/src/Diagnostic.cc index d12bdcf..f7d4264 100644 --- a/src/Diagnostic.cc +++ b/src/Diagnostic.cc @@ -6,20 +6,20 @@ clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) { severity=clang_getDiagnosticSeverity(cx_diagnostic); severity_spelling=get_severity_spelling(severity); - spelling=clang::to_string(clang_getDiagnosticSpelling(cx_diagnostic)); + spelling=to_string(clang_getDiagnosticSpelling(cx_diagnostic)); - clang::SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); + SourceLocation start_location(clang_getDiagnosticLocation(cx_diagnostic)); path=start_location.get_path(); auto start_offset=start_location.get_offset(); - clang::Tokens tokens(cx_tu, SourceRange(start_location, start_location)); + Tokens tokens(cx_tu, SourceRange(start_location, start_location)); if(tokens.size()==1) offsets={start_offset, tokens.begin()->offsets.second}; unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic); for(unsigned c=0;c(clang_getTokenKind(cx_token)); +clang::Token::Kind clang::Token::get_kind() { + return static_cast(clang_getTokenKind(cx_token)); } diff --git a/src/Token.h b/src/Token.h index e212961..f22c964 100644 --- a/src/Token.h +++ b/src/Token.h @@ -7,20 +7,21 @@ #include namespace clang { - enum TokenKind { - Token_Punctuation, - Token_Keyword, - Token_Identifier, - Token_Literal, - Token_Comment - }; - class Token { friend class Tokens; + public: + enum Kind { + Punctuation, + Keyword, + Identifier, + Literal, + Comment + }; + private: Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {}; public: - TokenKind get_kind(); + Kind get_kind(); std::string get_spelling(); SourceLocation get_source_location(); SourceRange get_source_range(); diff --git a/src/Tokens.cc b/src/Tokens.cc index d4f2892..1de9a07 100644 --- a/src/Tokens.cc +++ b/src/Tokens.cc @@ -23,12 +23,12 @@ clang::Tokens::~Tokens() { //This works across TranslationUnits! However, to get rename refactoring to work, //one have to open all the files that might include a similar token //Similar tokens defined as tokens with equal referenced cursors. -std::vector > clang::Tokens::get_similar_token_offsets(CursorKind kind, +std::vector > clang::Tokens::get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling, const std::string &usr) { - std::vector > offsets; + std::vector > offsets; for(auto &token: *this) { - if(token.get_kind()==clang::Token_Identifier) { + if(token.get_kind()==Token::Kind::Identifier) { auto referenced=token.get_cursor().get_referenced(); if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr()) offsets.emplace_back(token.offsets); @@ -38,19 +38,19 @@ std::vector > clang::Tokens::get_similar } std::vector > clang::Tokens::get_cxx_methods() { - std::vector > methods; - clang::Offset last_offset={(unsigned)-1,(unsigned) -1}; + std::vector > methods; + Offset last_offset={(unsigned)-1,(unsigned) -1}; for(auto &token: *this) { - if(token.get_kind()==clang::Token_Identifier) { + if(token.get_kind()==Token::Kind::Identifier) { auto cursor=token.get_cursor(); auto kind=cursor.get_kind(); - if(kind==clang::CursorKind::CXXMethod || kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) { + if(kind==Cursor::Kind::CXXMethod || kind==Cursor::Kind::Constructor || kind==Cursor::Kind::Destructor) { auto offset=cursor.get_source_location().get_offset(); if(offset!=last_offset) { std::string method; - if(kind==clang::CursorKind::CXXMethod) { + if(kind==Cursor::Kind::CXXMethod) { auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor)); - method+=clang::to_string(clang_getTypeSpelling(type)); + method+=to_string(clang_getTypeSpelling(type)); auto pos=method.find(" "); if(pos!=std::string::npos) method.erase(pos, 1); @@ -59,12 +59,12 @@ std::vector > clang::Tokens::get_cxx_metho std::string parent_str; auto parent=cursor.get_semantic_parent(); - while(parent && parent.get_kind()!=clang::CursorKind::TranslationUnit) { - parent_str.insert(0, clang::to_string(clang_getCursorDisplayName(parent.cx_cursor))+"::"); + while(parent && parent.get_kind()!=Cursor::Kind::TranslationUnit) { + parent_str.insert(0, to_string(clang_getCursorDisplayName(parent.cx_cursor))+"::"); parent=parent.get_semantic_parent(); } - method+=parent_str+clang::to_string(clang_getCursorDisplayName(cursor.cx_cursor)); + method+=parent_str+to_string(clang_getCursorDisplayName(cursor.cx_cursor)); methods.emplace_back(method, offset); } last_offset=offset; diff --git a/src/Tokens.h b/src/Tokens.h index a6f0648..13bd62a 100644 --- a/src/Tokens.h +++ b/src/Tokens.h @@ -13,7 +13,7 @@ namespace clang { Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); public: ~Tokens(); - std::vector > get_similar_token_offsets(CursorKind kind, + std::vector > get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling, const std::string &usr); std::vector > get_cxx_methods(); diff --git a/src/TranslationUnit.cc b/src/TranslationUnit.cc index ab8a713..1b16c37 100644 --- a/src/TranslationUnit.cc +++ b/src/TranslationUnit.cc @@ -63,7 +63,7 @@ void clang::TranslationUnit::parse(Index &index, const std::string &file_path, int clang::TranslationUnit::ReparseTranslationUnit(const std::string &buffer, unsigned flags) { CXUnsavedFile files[1]; - auto file_path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); + auto file_path=to_string(clang_getTranslationUnitSpelling(cx_tu)); files[0].Filename=file_path.c_str(); files[0].Contents=buffer.c_str(); @@ -78,15 +78,15 @@ unsigned clang::TranslationUnit::DefaultFlags() { clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::string &buffer, unsigned line_number, unsigned column) { - clang::CodeCompleteResults results(cx_tu, buffer, line_number, column); + CodeCompleteResults results(cx_tu, buffer, line_number, column); return results; } std::vector clang::TranslationUnit::get_diagnostics() { - std::vector diagnostics; + std::vector diagnostics; for(unsigned c=0;c clang::TranslationUnit::get_diagnostics() { std::unique_ptr clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) { auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); - clang::SourceLocation start_location(cx_tu, path, start_offset); - clang::SourceLocation end_location(cx_tu, path, end_offset); - clang::SourceRange range(start_location, end_location); + SourceLocation start_location(cx_tu, path, start_offset); + SourceLocation end_location(cx_tu, path, end_offset); + SourceRange range(start_location, end_location); return std::unique_ptr(new Tokens(cx_tu, range)); } std::unique_ptr clang::TranslationUnit::get_tokens(unsigned start_line, unsigned start_column, unsigned end_line, unsigned end_column) { - auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); - clang::SourceLocation start_location(cx_tu, path, start_line, start_column); - clang::SourceLocation end_location(cx_tu, path, end_line, end_column); - clang::SourceRange range(start_location, end_location); + auto path=to_string(clang_getTranslationUnitSpelling(cx_tu)); + SourceLocation start_location(cx_tu, path, start_line, start_column); + SourceLocation end_location(cx_tu, path, end_line, end_column); + SourceRange range(start_location, end_location); return std::unique_ptr(new Tokens(cx_tu, range)); } clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned offset) { - clang::SourceLocation location(cx_tu, path, offset); + SourceLocation location(cx_tu, path, offset); return Cursor(clang_getCursor(cx_tu, location.cx_location)); } clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned line, unsigned column) { - clang::SourceLocation location(cx_tu, path, line, column); + SourceLocation location(cx_tu, path, line, column); return Cursor(clang_getCursor(cx_tu, location.cx_location)); } diff --git a/tests/Cursor_H_Test.cc b/tests/Cursor_H_Test.cc index ad56efd..f179fcb 100644 --- a/tests/Cursor_H_Test.cc +++ b/tests/Cursor_H_Test.cc @@ -14,5 +14,5 @@ BOOST_AUTO_TEST_CASE(cursor) { auto cursor=tu.get_cursor(path, 103); - BOOST_CHECK(cursor.get_kind() == clang::CursorKind::ReturnStmt); + BOOST_CHECK(cursor.get_kind() == clang::Cursor::Kind::ReturnStmt); } diff --git a/tests/Token_H_Test.cc b/tests/Token_H_Test.cc index 91e8d48..adbade5 100644 --- a/tests/Token_H_Test.cc +++ b/tests/Token_H_Test.cc @@ -12,7 +12,7 @@ BOOST_AUTO_TEST_CASE(token) { auto tokens=tu.get_tokens(0, 113); BOOST_CHECK(tokens->size() == 32); - BOOST_CHECK((*tokens)[1].get_kind() == clang::TokenKind::Token_Identifier); + BOOST_CHECK((*tokens)[1].get_kind() == clang::Token::Kind::Identifier); std::string str = (*tokens)[28].get_spelling(); BOOST_CHECK(str == "return");