diff --git a/.clang-format b/.clang-format index bed34c0..d6d82b0 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,5 @@ # Generated from CLion C/C++ Code Style settings +BinPackArguments: false BinPackParameters: false BasedOnStyle: LLVM AccessModifierOffset: -4 @@ -17,6 +18,7 @@ AllowShortLoopsOnASingleLine: true AlwaysBreakTemplateDeclarations: Yes # 函数和返回类型分两行,方便阅读 AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBinaryOperators: true BreakBeforeBraces: Custom BraceWrapping: AfterCaseLabel: false @@ -36,7 +38,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon ConstructorInitializerAllOnOneLineOrOnePerLine: true BreakInheritanceList: BeforeColon -ColumnLimit: 80 +ColumnLimit: 100 CompactNamespaces: false ContinuationIndentWidth: 4 EmptyLineBeforeAccessModifier: LogicalBlock diff --git a/3rdparty/sled b/3rdparty/sled index 3d5039e..4b68474 160000 --- a/3rdparty/sled +++ b/3rdparty/sled @@ -1 +1 @@ -Subproject commit 3d5039ed9521b4e4ee5a376274eb13ca41bca758 +Subproject commit 4b68474fe58fb3af754f93916438bc8d2ba6aac1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d16712..95d680a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(meta src/main.cc src/clang/cursor.cc src/clang/cursor_type.cc + src/clang/parser.cc ) ### add clang diff --git a/src/clang/cursor.cc b/src/clang/cursor.cc index d30bf0c..387a49f 100644 --- a/src/clang/cursor.cc +++ b/src/clang/cursor.cc @@ -1,51 +1,71 @@ #include "cursor.h" #include "utils.h" + namespace meta { - Cursor::Cursor(const CXCursor& handle) : handle_(handle) {} - CXCursorKind Cursor::GetKind(void) { - return handle_.kind; - } - std::string Cursor::GetSpelling(void) const { - return ToString(clang_getCursorSpelling(handle_)); - } +Cursor::Cursor(const CXCursor &handle) : handle_(handle) {} - std::string Cursor::GetDisplayName(void) const{ - return ToString(clang_getCursorDisplayName(handle_)); - } - - std::string Cursor::GetSourceFile(void) const{ - auto range = clang_Cursor_getSpellingNameRange(handle_, 0, 0); - auto start = clang_getRangeStart(range); - CXFile file; - unsigned line, column, offset; - clang_getFileLocation(start, &file, &line, &column, &offset); - return ToString(clang_getFileName(file)); - } - - bool Cursor::IsDefinition(void) const{ - return clang_isCursorDefinition(handle_); - } - - CursorType Cursor::GetType(void) const{ - return clang_getCursorType(handle_); - } - - Cursor::List Cursor::GetChildren(void) const{ - List children; - - clang_visitChildren(handle_, [](CXCursor cursor, CXCursor parent, CXClientData client_data) { - auto container = static_cast(client_data); - container->push_back(cursor); - if (cursor.kind == CXCursor_LastPreprocessing) { - return CXChildVisit_Recurse; - } - return CXChildVisit_Continue; - }, &children); - - return children; - } - - void Cursor::VisitChildren(Visitor visitor, void *data) const{ - clang_visitChildren(handle_, visitor, data); - } +CXCursorKind +Cursor::GetKind(void) +{ + return handle_.kind; } + +std::string +Cursor::GetSpelling(void) const +{ + return ToString(clang_getCursorSpelling(handle_)); +} + +std::string +Cursor::GetDisplayName(void) const +{ + return ToString(clang_getCursorDisplayName(handle_)); +} + +std::string +Cursor::GetSourceFile(void) const +{ + auto range = clang_Cursor_getSpellingNameRange(handle_, 0, 0); + auto start = clang_getRangeStart(range); + CXFile file; + unsigned line, column, offset; + clang_getFileLocation(start, &file, &line, &column, &offset); + return ToString(clang_getFileName(file)); +} + +bool +Cursor::IsDefinition(void) const +{ + return clang_isCursorDefinition(handle_); +} + +CursorType +Cursor::GetType(void) const +{ + return clang_getCursorType(handle_); +} + +Cursor::List +Cursor::GetChildren(void) const +{ + List children; + + clang_visitChildren( + handle_, + [](CXCursor cursor, CXCursor parent, CXClientData client_data) { + auto container = static_cast(client_data); + container->push_back(cursor); + if (cursor.kind == CXCursor_LastPreprocessing) { return CXChildVisit_Recurse; } + return CXChildVisit_Continue; + }, + &children); + + return children; +} + +void +Cursor::VisitChildren(Visitor visitor, void *data) const +{ + clang_visitChildren(handle_, visitor, data); +} +}// namespace meta diff --git a/src/clang/cursor.h b/src/clang/cursor.h index e460a5f..8de9834 100644 --- a/src/clang/cursor.h +++ b/src/clang/cursor.h @@ -11,7 +11,7 @@ public: using List = std::vector; using Visitor = CXCursorVisitor; - Cursor(const CXCursor& handle); + Cursor(const CXCursor &handle); CXCursorKind GetKind(void); std::string GetSpelling(void) const; @@ -20,13 +20,12 @@ public: bool IsDefinition(void) const; CursorType GetType(void) const; List GetChildren(void) const; - void VisitChildren(Visitor visitor, void* data) const; + void VisitChildren(Visitor visitor, void *data) const; private: CXCursor handle_; }; +}// namespace meta -} // namespace meta; - -#endif // META_CLANG_CURSOR_H +#endif// META_CLANG_CURSOR_H diff --git a/src/clang/cursor_type.cc b/src/clang/cursor_type.cc index dc1d907..ec1f541 100644 --- a/src/clang/cursor_type.cc +++ b/src/clang/cursor_type.cc @@ -1,24 +1,50 @@ #include "cursor_type.h" #include "cursor.h" #include "utils.h" + namespace meta { -CursorType::CursorType(const CXType& handle) : handle_(handle) {} +CursorType::CursorType(const CXType &handle) : handle_(handle) {} -std::string CursorType::GetDisplayName(void) const +std::string +CursorType::GetDisplayName(void) const { return ToString(clang_getTypeSpelling(handle_)); } -int CursorType::GetArgumentCount(void) const { return clang_getNumArgTypes(handle_); } - -CursorType CursorType::GetArgument(unsigned index) const { return clang_getArgType(handle_, index); } - -CursorType CursorType::GetCanonicalType(void) const { return clang_getCanonicalType(handle_); } - -Cursor CursorType::GetDeclaration(void) const { return clang_getTypeDeclaration(handle_); } - -CXTypeKind CursorType::GetKind(void) const { return handle_.kind; } - -bool CursorType::IsConst(void) const { return clang_isConstQualifiedType(handle_) ? true : false; } +int +CursorType::GetArgumentCount(void) const +{ + return clang_getNumArgTypes(handle_); } + +CursorType +CursorType::GetArgument(unsigned index) const +{ + return clang_getArgType(handle_, index); +} + +CursorType +CursorType::GetCanonicalType(void) const +{ + return clang_getCanonicalType(handle_); +} + +Cursor +CursorType::GetDeclaration(void) const +{ + return clang_getTypeDeclaration(handle_); +} + +CXTypeKind +CursorType::GetKind(void) const +{ + return handle_.kind; +} + +bool +CursorType::IsConst(void) const +{ + return clang_isConstQualifiedType(handle_) ? true : false; +} +}// namespace meta diff --git a/src/clang/cursor_type.h b/src/clang/cursor_type.h index 6e65b99..ac2ad38 100644 --- a/src/clang/cursor_type.h +++ b/src/clang/cursor_type.h @@ -10,11 +10,13 @@ extern "C" { #endif #include + namespace meta { class Cursor; + class CursorType { public: - CursorType(const CXType& handle); + CursorType(const CXType &handle); std::string GetDisplayName(void) const; int GetArgumentCount(void) const; @@ -27,8 +29,5 @@ public: private: CXType handle_; }; -} -#endif // META_CLANG_CURSOR_TYPE_H - - - +}// namespace meta +#endif// META_CLANG_CURSOR_TYPE_H diff --git a/src/clang/parser.cc b/src/clang/parser.cc new file mode 100644 index 0000000..0b20483 --- /dev/null +++ b/src/clang/parser.cc @@ -0,0 +1,48 @@ +#include "parser.h" +#include + +namespace meta { +Parser::Parser() : index_(nullptr), translation_unit_(nullptr) {} + +Parser::~Parser() +{ + clang_disposeTranslationUnit(translation_unit_); + clang_disposeIndex(index_); + // translation_unit_ = nullptr; + // index_ = nullptr; +} + +bool +Parser::Parse(const std::string &file_name) +{ + const char *args[] = {"-std=c++11"}; + + if (translation_unit_) { + clang_disposeTranslationUnit(translation_unit_); + translation_unit_ = nullptr; + } + + if (index_) { + clang_disposeIndex(index_); + index_ = nullptr; + } + + index_ = clang_createIndex(0, 0); + translation_unit_ = clang_parseTranslationUnit(index_, + file_name.c_str(), + kDefaultArguments.data(), + kDefaultArguments.size(), + nullptr, + 0, + CXTranslationUnit_None); + return translation_unit_ != nullptr; +} + +Cursor +Parser::GetCursor(void) const +{ + ASSERT(translation_unit_, "translation_unit_ is nullptr"); + return clang_getTranslationUnitCursor(translation_unit_); +}; + +}// namespace meta diff --git a/src/clang/parser.h b/src/clang/parser.h new file mode 100644 index 0000000..0f59d4e --- /dev/null +++ b/src/clang/parser.h @@ -0,0 +1,37 @@ +#pragma once +#ifndef META_CLANG_PARSER_H +#define META_CLANG_PARSER_H + +#include "cursor.h" +#include "cursor_type.h" +#include + +namespace meta { +class Parser final { +public: + Parser(); + ~Parser(); + + bool Parse(const std::string &file_name); + Cursor GetCursor(void) const; + +private: + CXIndex index_; + CXTranslationUnit translation_unit_; + +private: + std::vector kDefaultArguments = {{"-x", + "c++", + "-std=c++11", + "-D__REFLECTION_PARSER__", + "-DNDEBUG", + "-D__clang__", + "-w", + "-MG", + "-M", + "-ferror-limit=0", + "-o clangLog.txt"}}; +}; +}// namespace meta + +#endif// META_CLANG_PARSER_H diff --git a/src/main.cc b/src/main.cc index 052a8ac..9153a03 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,26 +1,29 @@ +#include "clang/parser.h" #include -#include "clang/cursor.h" -const char* kTag = "main"; +const char *kTag = "main"; -int main(int argc, char* argv[]) { +int +main(int argc, char *argv[]) +{ LOGI(kTag, ""); - CXIndex index = clang_createIndex(0, 0); - CXTranslationUnit unit = clang_parseTranslationUnit(index, "/tmp/main.cc", nullptr, 0, nullptr, 0, CXTranslationUnit_None); + meta::Parser parser; - CXString file_name = clang_getTranslationUnitSpelling(unit); - LOGI(kTag, "file_name={}", clang_getCString(file_name)); + parser.Parse("/tmp/main.cc"); - meta::Cursor cursor = clang_getTranslationUnitCursor(unit); - for (auto& child : cursor.GetChildren()) { + meta::Cursor cursor = parser.GetCursor(); + for (auto &child : cursor.GetChildren()) { auto kind = child.GetKind(); if (child.IsDefinition() && (kind == CXCursor_ClassDecl || kind == CXCursor_StructDecl)) { - LOGV(kTag, "class={}", child.GetDisplayName()); + LOGV(kTag, + "class={}, file={}, spelling={}", + child.GetDisplayName(), + child.GetSourceFile(), + child.GetSpelling()); + } else if (kind == CXCursor_Namespace) { } } - clang_disposeTranslationUnit(unit); - clang_disposeIndex(index); return 0; }