feat update .clang-format

This commit is contained in:
tqcq 2024-03-03 00:16:56 +08:00
parent 9485047cc2
commit abe514fcd5
10 changed files with 219 additions and 84 deletions

View File

@ -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

2
3rdparty/sled vendored

@ -1 +1 @@
Subproject commit 3d5039ed9521b4e4ee5a376274eb13ca41bca758
Subproject commit 4b68474fe58fb3af754f93916438bc8d2ba6aac1

View File

@ -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

View File

@ -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<List*>(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<List *>(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

View File

@ -11,7 +11,7 @@ public:
using List = std::vector<Cursor>;
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

View File

@ -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

View File

@ -10,11 +10,13 @@ extern "C" {
#endif
#include <vector>
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

48
src/clang/parser.cc Normal file
View File

@ -0,0 +1,48 @@
#include "parser.h"
#include <sled/log/log.h>
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

37
src/clang/parser.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#ifndef META_CLANG_PARSER_H
#define META_CLANG_PARSER_H
#include "cursor.h"
#include "cursor_type.h"
#include <string>
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<const char *> 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

View File

@ -1,26 +1,29 @@
#include "clang/parser.h"
#include <sled/log/log.h>
#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;
}