Browse Source

Cleanup, added Cursor::Type class, and extra functions to Cursor

merge-requests/37/head
eidheim 10 years ago
parent
commit
ca2021d935
  1. 4
      src/CodeCompleteResults.cc
  2. 6
      src/CompilationDatabase.cc
  3. 1
      src/CompilationDatabase.h
  4. 10
      src/CompileCommand.cc
  5. 11
      src/CompileCommands.cc
  6. 9
      src/CompletionString.cc
  7. 54
      src/Cursor.cc
  8. 356
      src/Cursor.h
  9. 10
      src/Diagnostic.cc
  10. 3
      src/Index.cc
  11. 2
      src/SourceLocation.cc
  12. 3
      src/SourceRange.cc
  13. 6
      src/Token.cc
  14. 19
      src/Token.h
  15. 24
      src/Tokens.cc
  16. 2
      src/Tokens.h
  17. 26
      src/TranslationUnit.cc
  18. 2
      tests/Cursor_H_Test.cc
  19. 2
      tests/Token_H_Test.cc

4
src/CodeCompleteResults.cc

@ -7,7 +7,7 @@ clang::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
const std::string &buffer, const std::string &buffer,
unsigned line_num, unsigned column) { unsigned line_num, unsigned column) {
CXUnsavedFile files[1]; 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].Filename = file_path.c_str();
files[0].Contents = buffer.c_str(); files[0].Contents = buffer.c_str();
files[0].Length = buffer.size(); files[0].Length = buffer.size();
@ -41,5 +41,5 @@ clang::CompletionString clang::CodeCompleteResults::get(unsigned i) const {
} }
std::string clang::CodeCompleteResults::get_usr() const { std::string clang::CodeCompleteResults::get_usr() const {
return clang::to_string(clang_codeCompleteGetContainerUSR(cx_results)); return to_string(clang_codeCompleteGetContainerUSR(cx_results));
} }

6
src/CompilationDatabase.cc

@ -1,8 +1,7 @@
#include "CompilationDatabase.h" #include "CompilationDatabase.h"
#include <exception> #include <exception>
clang::CompilationDatabase:: clang::CompilationDatabase::CompilationDatabase(const std::string &project_path) {
CompilationDatabase(const std::string &project_path) {
CXCompilationDatabase_Error error; CXCompilationDatabase_Error error;
cx_db = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error); cx_db = clang_CompilationDatabase_fromDirectory(project_path.c_str(), &error);
if(error) { if(error) {
@ -10,7 +9,6 @@ CompilationDatabase(const std::string &project_path) {
} }
} }
clang::CompilationDatabase:: clang::CompilationDatabase::~CompilationDatabase() {
~CompilationDatabase() {
clang_CompilationDatabase_dispose(cx_db); clang_CompilationDatabase_dispose(cx_db);
} }

1
src/CompilationDatabase.h

@ -8,7 +8,6 @@ namespace clang {
class CompilationDatabase { class CompilationDatabase {
public: public:
explicit CompilationDatabase(const std::string &project_path); explicit CompilationDatabase(const std::string &project_path);
CompilationDatabase();
~CompilationDatabase(); ~CompilationDatabase();
CXCompilationDatabase cx_db; CXCompilationDatabase cx_db;

10
src/CompileCommand.cc

@ -2,22 +2,20 @@
#include "CompileCommands.h" #include "CompileCommands.h"
#include "Utility.h" #include "Utility.h"
std::string clang::CompileCommand:: std::string clang::CompileCommand::get_command() {
get_command() {
std::string res; std::string res;
unsigned N = clang_CompileCommand_getNumArgs(cx_command); unsigned N = clang_CompileCommand_getNumArgs(cx_command);
for (unsigned i = 0; i < N; i++) { 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; return res;
} }
std::vector<std::string> clang::CompileCommand:: std::vector<std::string> clang::CompileCommand::get_command_as_args() {
get_command_as_args() {
unsigned N = clang_CompileCommand_getNumArgs(cx_command); unsigned N = clang_CompileCommand_getNumArgs(cx_command);
std::vector<std::string> res(N); std::vector<std::string> res(N);
for (unsigned i = 0; i < N; i++) { 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; return res;
} }

11
src/CompileCommands.cc

@ -1,22 +1,19 @@
#include "CompileCommands.h" #include "CompileCommands.h"
clang::CompileCommands:: clang::CompileCommands::CompileCommands(const std::string &filename, CompilationDatabase &db) {
CompileCommands(const std::string &filename, CompilationDatabase &db) {
cx_commands = cx_commands =
clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str()); clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str());
if(clang_CompileCommands_getSize(cx_commands)==0) if(clang_CompileCommands_getSize(cx_commands)==0)
cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db); cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db);
} }
clang::CompileCommands:: clang::CompileCommands::~CompileCommands() {
~CompileCommands() {
clang_CompileCommands_dispose(cx_commands); clang_CompileCommands_dispose(cx_commands);
} }
std::vector<clang::CompileCommand> clang::CompileCommands:: std::vector<clang::CompileCommand> clang::CompileCommands::get_commands() {
get_commands() {
unsigned N = clang_CompileCommands_getSize(cx_commands); unsigned N = clang_CompileCommands_getSize(cx_commands);
std::vector<clang::CompileCommand> res; std::vector<CompileCommand> res;
for (unsigned i = 0; i < N; i++) { for (unsigned i = 0; i < N; i++) {
res.emplace_back(clang_CompileCommands_getCommand(cx_commands, i)); res.emplace_back(clang_CompileCommands_getCommand(cx_commands, i));
} }

9
src/CompletionString.cc

@ -13,17 +13,16 @@ unsigned clang::CompletionString::get_num_chunks() {
} }
std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() { std::vector<clang::CompletionChunk> clang::CompletionString::get_chunks() {
std::vector<clang::CompletionChunk> res; std::vector<CompletionChunk> res;
for (unsigned i = 0; i < get_num_chunks(); i++) { for (unsigned i = 0; i < get_num_chunks(); i++) {
res.emplace_back(clang::to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_completion_sting, i))); res.emplace_back(to_string(clang_getCompletionChunkText(cx_completion_sting, i)), static_cast<CompletionChunkKind> (clang_getCompletionChunkKind(cx_completion_sting, i)));
} }
return res; return res;
} }
std::string clang::CompletionString::get_brief_comments() { 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:: clang::CompletionChunk::CompletionChunk(std::string chunk, CompletionChunkKind kind) :
CompletionChunk(std::string chunk, clang::CompletionChunkKind kind) :
chunk(chunk), kind(kind) { } chunk(chunk), kind(kind) { }

54
src/Cursor.cc

@ -2,8 +2,24 @@
#include "Utility.h" #include "Utility.h"
#include <algorithm> #include <algorithm>
clang::CursorKind clang::Cursor::get_kind() { std::string clang::Cursor::Type::get_spelling() const {
return static_cast<CursorKind>(clang_getCursorKind(this->cx_cursor)); 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<Kind>(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 { 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 { 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 { 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 { clang::Cursor clang::Cursor::get_referenced() const {
return Cursor(clang_getCursorReferenced(cx_cursor)); 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 { 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> clang::Cursor::get_arguments() const {
std::vector<Cursor> cursors;
auto size=clang_Cursor_getNumArguments(cx_cursor);
for(int c=0;c<size;++c)
cursors.emplace_back(clang_Cursor_getArgument(cx_cursor, c));
return cursors;
} }
clang::Cursor::operator bool() const { clang::Cursor::operator bool() const {
@ -38,7 +70,7 @@ bool clang::Cursor::operator==(const Cursor& rhs) const {
return clang_equalCursors(cx_cursor, rhs.cx_cursor); return clang_equalCursors(cx_cursor, rhs.cx_cursor);
} }
bool clang::Cursor::has_type() { bool clang::Cursor::has_type_description() {
auto referenced=clang_getCursorReferenced(cx_cursor); auto referenced=clang_getCursorReferenced(cx_cursor);
if(clang_Cursor_isNull(referenced)) if(clang_Cursor_isNull(referenced))
return false; return false;
@ -46,18 +78,18 @@ bool clang::Cursor::has_type() {
return type.kind!=0; return type.kind!=0;
} }
std::string clang::Cursor::get_type() { std::string clang::Cursor::get_type_description() {
std::string spelling; std::string spelling;
auto referenced=clang_getCursorReferenced(cx_cursor); auto referenced=clang_getCursorReferenced(cx_cursor);
if(!clang_Cursor_isNull(referenced)) { if(!clang_Cursor_isNull(referenced)) {
auto type=clang_getCursorType(referenced); auto type=clang_getCursorType(referenced);
spelling=clang::to_string(clang_getTypeSpelling(type)); spelling=to_string(clang_getTypeSpelling(type));
#if CINDEX_VERSION_MAJOR==0 && CINDEX_VERSION_MINOR<32 #if CINDEX_VERSION_MAJOR==0 && CINDEX_VERSION_MINOR<32
const std::string auto_str="auto"; const std::string auto_str="auto";
if(spelling.size()>=4 && std::equal(auto_str.begin(), auto_str.end(), spelling.begin())) { if(spelling.size()>=4 && std::equal(auto_str.begin(), auto_str.end(), spelling.begin())) {
auto canonical_type=clang_getCanonicalType(clang_getCursorType(cx_cursor)); 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) if(spelling.size()>5 && spelling[4]==' ' && spelling[5]=='&' && spelling!=canonical_spelling)
return canonical_spelling+" &"; return canonical_spelling+" &";
else else
@ -67,7 +99,7 @@ std::string clang::Cursor::get_type() {
const std::string const_auto_str="const auto"; const std::string const_auto_str="const auto";
if(spelling.size()>=10 && std::equal(const_auto_str.begin(), const_auto_str.end(), spelling.begin())) { 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_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) if(spelling.size()>11 && spelling[10]==' ' && spelling[11]=='&' && spelling!=canonical_spelling)
return canonical_spelling+" &"; return canonical_spelling+" &";
else else
@ -83,7 +115,7 @@ std::string clang::Cursor::get_brief_comments() {
std::string comment_string; std::string comment_string;
auto referenced=get_referenced(); auto referenced=get_referenced();
if(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; return comment_string;
} }

356
src/Cursor.h

@ -4,192 +4,206 @@
#include "SourceLocation.h" #include "SourceLocation.h"
#include "SourceRange.h" #include "SourceRange.h"
#include <string> #include <string>
#include <vector>
namespace clang { 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,
};
class Cursor { class Cursor {
public: 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() { cx_cursor=clang_getNullCursor(); }
Cursor(const CXCursor &cx_cursor) : cx_cursor(cx_cursor) {} 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; SourceLocation get_source_location() const;
SourceRange get_source_range() const; SourceRange get_source_range() const;
std::string get_spelling() const; std::string get_spelling() const;
std::string get_usr() const; std::string get_usr() const;
Cursor get_referenced() const; Cursor get_referenced() const;
Cursor get_canonical() const;
Cursor get_definition() const;
Cursor get_semantic_parent() const; Cursor get_semantic_parent() const;
std::vector<Cursor> get_arguments() const;
operator bool() const; operator bool() const;
bool operator==(const Cursor& rhs) const; bool operator==(const Cursor& rhs) const;
bool has_type(); bool has_type_description();
std::string get_type(); std::string get_type_description();
std::string get_brief_comments(); std::string get_brief_comments();
CXCursor cx_cursor; CXCursor cx_cursor;

10
src/Diagnostic.cc

@ -6,20 +6,20 @@
clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) { clang::Diagnostic::Diagnostic(CXTranslationUnit& cx_tu, CXDiagnostic& cx_diagnostic) {
severity=clang_getDiagnosticSeverity(cx_diagnostic); severity=clang_getDiagnosticSeverity(cx_diagnostic);
severity_spelling=get_severity_spelling(severity); 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(); path=start_location.get_path();
auto start_offset=start_location.get_offset(); 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) if(tokens.size()==1)
offsets={start_offset, tokens.begin()->offsets.second}; offsets={start_offset, tokens.begin()->offsets.second};
unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic); unsigned num_fix_its=clang_getDiagnosticNumFixIts(cx_diagnostic);
for(unsigned c=0;c<num_fix_its;c++) { for(unsigned c=0;c<num_fix_its;c++) {
CXSourceRange fix_it_range; CXSourceRange fix_it_range;
auto source=clang::to_string(clang_getDiagnosticFixIt(cx_diagnostic, c, &fix_it_range)); auto source=to_string(clang_getDiagnosticFixIt(cx_diagnostic, c, &fix_it_range));
fix_its.emplace_back(source, clang::SourceRange(fix_it_range).get_offsets()); fix_its.emplace_back(source, SourceRange(fix_it_range).get_offsets());
} }
} }

3
src/Index.cc

@ -1,7 +1,6 @@
#include "Index.h" #include "Index.h"
clang::Index:: clang::Index::Index(int excludeDeclarationsFromPCH, int displayDiagnostics) {
Index(int excludeDeclarationsFromPCH, int displayDiagnostics) {
cx_index = clang_createIndex(excludeDeclarationsFromPCH, displayDiagnostics); cx_index = clang_createIndex(excludeDeclarationsFromPCH, displayDiagnostics);
} }

2
src/SourceLocation.cc

@ -32,7 +32,7 @@ void clang::SourceLocation::get_data(std::string* path, unsigned *line, unsigned
CXFile file; CXFile file;
clang_getExpansionLocation(cx_location, &file, line, column, offset); clang_getExpansionLocation(cx_location, &file, line, column, offset);
if (file!=NULL) { if (file!=NULL) {
*path=clang::to_string(clang_getFileName(file)); *path=to_string(clang_getFileName(file));
} }
} }
} }

3
src/SourceRange.cc

@ -1,7 +1,6 @@
#include "SourceRange.h" #include "SourceRange.h"
clang::SourceRange:: clang::SourceRange::SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) {
SourceRange(clang::SourceLocation &start, clang::SourceLocation &end) {
cx_range = clang_getRange(start.cx_location, end.cx_location); cx_range = clang_getRange(start.cx_location, end.cx_location);
} }

6
src/Token.cc

@ -17,9 +17,9 @@ clang::SourceRange clang::Token::get_source_range() {
} }
// returns a string description of this tokens kind // returns a string description of this tokens kind
std::string clang::Token::get_spelling() { std::string clang::Token::get_spelling() {
return clang::to_string(clang_getTokenSpelling(cx_tu, cx_token)); return to_string(clang_getTokenSpelling(cx_tu, cx_token));
} }
clang::TokenKind clang::Token::get_kind() { clang::Token::Kind clang::Token::get_kind() {
return static_cast<TokenKind>(clang_getTokenKind(cx_token)); return static_cast<Kind>(clang_getTokenKind(cx_token));
} }

19
src/Token.h

@ -7,20 +7,21 @@
#include <string> #include <string>
namespace clang { namespace clang {
enum TokenKind {
Token_Punctuation,
Token_Keyword,
Token_Identifier,
Token_Literal,
Token_Comment
};
class Token { class Token {
friend class Tokens; friend class Tokens;
public:
enum Kind {
Punctuation,
Keyword,
Identifier,
Literal,
Comment
};
private:
Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): 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()) {}; cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor), offsets(get_source_range().get_offsets()) {};
public: public:
TokenKind get_kind(); Kind get_kind();
std::string get_spelling(); std::string get_spelling();
SourceLocation get_source_location(); SourceLocation get_source_location();
SourceRange get_source_range(); SourceRange get_source_range();

24
src/Tokens.cc

@ -23,12 +23,12 @@ clang::Tokens::~Tokens() {
//This works across TranslationUnits! However, to get rename refactoring to work, //This works across TranslationUnits! However, to get rename refactoring to work,
//one have to open all the files that might include a similar token //one have to open all the files that might include a similar token
//Similar tokens defined as tokens with equal referenced cursors. //Similar tokens defined as tokens with equal referenced cursors.
std::vector<std::pair<clang::Offset, clang::Offset> > clang::Tokens::get_similar_token_offsets(CursorKind kind, std::vector<std::pair<clang::Offset, clang::Offset> > clang::Tokens::get_similar_token_offsets(Cursor::Kind kind,
const std::string &spelling, const std::string &spelling,
const std::string &usr) { const std::string &usr) {
std::vector<std::pair<clang::Offset, clang::Offset> > offsets; std::vector<std::pair<Offset, Offset> > offsets;
for(auto &token: *this) { 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(); auto referenced=token.get_cursor().get_referenced();
if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr()) if(referenced && kind==referenced.get_kind() && spelling==token.get_spelling() && usr==referenced.get_usr())
offsets.emplace_back(token.offsets); offsets.emplace_back(token.offsets);
@ -38,19 +38,19 @@ std::vector<std::pair<clang::Offset, clang::Offset> > clang::Tokens::get_similar
} }
std::vector<std::pair<std::string, clang::Offset> > clang::Tokens::get_cxx_methods() { std::vector<std::pair<std::string, clang::Offset> > clang::Tokens::get_cxx_methods() {
std::vector<std::pair<std::string, clang::Offset> > methods; std::vector<std::pair<std::string, Offset> > methods;
clang::Offset last_offset={(unsigned)-1,(unsigned) -1}; Offset last_offset={(unsigned)-1,(unsigned) -1};
for(auto &token: *this) { for(auto &token: *this) {
if(token.get_kind()==clang::Token_Identifier) { if(token.get_kind()==Token::Kind::Identifier) {
auto cursor=token.get_cursor(); auto cursor=token.get_cursor();
auto kind=cursor.get_kind(); 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(); auto offset=cursor.get_source_location().get_offset();
if(offset!=last_offset) { if(offset!=last_offset) {
std::string method; std::string method;
if(kind==clang::CursorKind::CXXMethod) { if(kind==Cursor::Kind::CXXMethod) {
auto type=clang_getResultType(clang_getCursorType(cursor.cx_cursor)); 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(" "); auto pos=method.find(" ");
if(pos!=std::string::npos) if(pos!=std::string::npos)
method.erase(pos, 1); method.erase(pos, 1);
@ -59,12 +59,12 @@ std::vector<std::pair<std::string, clang::Offset> > clang::Tokens::get_cxx_metho
std::string parent_str; std::string parent_str;
auto parent=cursor.get_semantic_parent(); auto parent=cursor.get_semantic_parent();
while(parent && parent.get_kind()!=clang::CursorKind::TranslationUnit) { while(parent && parent.get_kind()!=Cursor::Kind::TranslationUnit) {
parent_str.insert(0, clang::to_string(clang_getCursorDisplayName(parent.cx_cursor))+"::"); parent_str.insert(0, to_string(clang_getCursorDisplayName(parent.cx_cursor))+"::");
parent=parent.get_semantic_parent(); 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); methods.emplace_back(method, offset);
} }
last_offset=offset; last_offset=offset;

2
src/Tokens.h

@ -13,7 +13,7 @@ namespace clang {
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range); Tokens(CXTranslationUnit &cx_tu, const SourceRange &range);
public: public:
~Tokens(); ~Tokens();
std::vector<std::pair<clang::Offset, clang::Offset> > get_similar_token_offsets(CursorKind kind, std::vector<std::pair<clang::Offset, clang::Offset> > get_similar_token_offsets(Cursor::Kind kind,
const std::string &spelling, const std::string &spelling,
const std::string &usr); const std::string &usr);
std::vector<std::pair<std::string, clang::Offset> > get_cxx_methods(); std::vector<std::pair<std::string, clang::Offset> > get_cxx_methods();

26
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) { int clang::TranslationUnit::ReparseTranslationUnit(const std::string &buffer, unsigned flags) {
CXUnsavedFile files[1]; 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].Filename=file_path.c_str();
files[0].Contents=buffer.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, clang::CodeCompleteResults clang::TranslationUnit::get_code_completions(const std::string &buffer,
unsigned line_number, unsigned column) { unsigned line_number, unsigned column) {
clang::CodeCompleteResults results(cx_tu, buffer, line_number, column); CodeCompleteResults results(cx_tu, buffer, line_number, column);
return results; return results;
} }
std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() { std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
std::vector<clang::Diagnostic> diagnostics; std::vector<Diagnostic> diagnostics;
for(unsigned c=0;c<clang_getNumDiagnostics(cx_tu);c++) { for(unsigned c=0;c<clang_getNumDiagnostics(cx_tu);c++) {
CXDiagnostic clang_diagnostic=clang_getDiagnostic(cx_tu, c); CXDiagnostic clang_diagnostic=clang_getDiagnostic(cx_tu, c);
diagnostics.emplace_back(clang::Diagnostic(cx_tu, clang_diagnostic)); diagnostics.emplace_back(Diagnostic(cx_tu, clang_diagnostic));
clang_disposeDiagnostic(clang_diagnostic); clang_disposeDiagnostic(clang_diagnostic);
} }
return diagnostics; return diagnostics;
@ -94,27 +94,27 @@ std::vector<clang::Diagnostic> clang::TranslationUnit::get_diagnostics() {
std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) { std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_offset, unsigned end_offset) {
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::SourceLocation start_location(cx_tu, path, start_offset); SourceLocation start_location(cx_tu, path, start_offset);
clang::SourceLocation end_location(cx_tu, path, end_offset); SourceLocation end_location(cx_tu, path, end_offset);
clang::SourceRange range(start_location, end_location); SourceRange range(start_location, end_location);
return std::unique_ptr<Tokens>(new Tokens(cx_tu, range)); return std::unique_ptr<Tokens>(new Tokens(cx_tu, range));
} }
std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_line, unsigned start_column, std::unique_ptr<clang::Tokens> clang::TranslationUnit::get_tokens(unsigned start_line, unsigned start_column,
unsigned end_line, unsigned end_column) { unsigned end_line, unsigned end_column) {
auto path=clang::to_string(clang_getTranslationUnitSpelling(cx_tu)); auto path=to_string(clang_getTranslationUnitSpelling(cx_tu));
clang::SourceLocation start_location(cx_tu, path, start_line, start_column); SourceLocation start_location(cx_tu, path, start_line, start_column);
clang::SourceLocation end_location(cx_tu, path, end_line, end_column); SourceLocation end_location(cx_tu, path, end_line, end_column);
clang::SourceRange range(start_location, end_location); SourceRange range(start_location, end_location);
return std::unique_ptr<Tokens>(new Tokens(cx_tu, range)); return std::unique_ptr<Tokens>(new Tokens(cx_tu, range));
} }
clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned offset) { 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)); return Cursor(clang_getCursor(cx_tu, location.cx_location));
} }
clang::Cursor clang::TranslationUnit::get_cursor(std::string path, unsigned line, unsigned column) { 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)); return Cursor(clang_getCursor(cx_tu, location.cx_location));
} }

2
tests/Cursor_H_Test.cc

@ -14,5 +14,5 @@ BOOST_AUTO_TEST_CASE(cursor) {
auto cursor=tu.get_cursor(path, 103); auto cursor=tu.get_cursor(path, 103);
BOOST_CHECK(cursor.get_kind() == clang::CursorKind::ReturnStmt); BOOST_CHECK(cursor.get_kind() == clang::Cursor::Kind::ReturnStmt);
} }

2
tests/Token_H_Test.cc

@ -12,7 +12,7 @@ BOOST_AUTO_TEST_CASE(token) {
auto tokens=tu.get_tokens(0, 113); auto tokens=tu.get_tokens(0, 113);
BOOST_CHECK(tokens->size() == 32); 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(); std::string str = (*tokens)[28].get_spelling();
BOOST_CHECK(str == "return"); BOOST_CHECK(str == "return");

Loading…
Cancel
Save