|
| 1 | +#include "parse/ast_visitor.h" |
| 2 | + |
| 3 | +#include "parse/util.h" |
| 4 | + |
| 5 | +namespace clang { |
| 6 | +namespace tooling { |
| 7 | +ASTVisitor::ASTVisitor(const ASTContext* context) : context_(context) { |
| 8 | +} |
| 9 | + |
| 10 | +bool ASTVisitor::VisitCXXRecordDecl(const CXXRecordDecl* decl) { |
| 11 | + if (decl->isThisDeclarationADefinition() && |
| 12 | + IsProjectFile(context_->getSourceManager(), decl->getLocation())) { |
| 13 | + ClassInfo cls; |
| 14 | + cls.name = decl->getNameAsString(); |
| 15 | + cls.kind = decl->getKindName(); |
| 16 | + cls.comment = GetComment(decl, *context_); |
| 17 | + cls.ns = GetEnclosingNamespace(decl); |
| 18 | + |
| 19 | + SourceLocation loc = decl->getLocation(); |
| 20 | + const SourceManager& src_manager = context_->getSourceManager(); |
| 21 | + PresumedLoc presu_loc = src_manager.getPresumedLoc(loc); |
| 22 | + if (!presu_loc.isInvalid()) { |
| 23 | + cls.location.file = presu_loc.getFilename(); |
| 24 | + cls.location.line = presu_loc.getLine(); |
| 25 | + cls.location.column = presu_loc.getColumn(); |
| 26 | + } |
| 27 | + |
| 28 | + for (const auto& base : decl->bases()) { |
| 29 | + std::string baseType = base.getType().getAsString(); |
| 30 | + cls.bases.emplace_back(baseType); |
| 31 | + } |
| 32 | + |
| 33 | + for (auto field : decl->fields()) { |
| 34 | + FieldInfo f; |
| 35 | + f.name = field->getNameAsString(); |
| 36 | + f.type = field->getType().getAsString(); |
| 37 | + f.access = AccessSpecifierToString(field->getAccess()); |
| 38 | + cls.fields.emplace_back(f); |
| 39 | + } |
| 40 | + |
| 41 | + for (auto method : decl->methods()) { |
| 42 | + if (!method->isImplicit()) { |
| 43 | + MethodInfo m; |
| 44 | + m.name = method->getNameAsString(); |
| 45 | + m.return_type = method->getReturnType().getAsString(); |
| 46 | + m.is_const = method->isConst(); |
| 47 | + m.is_static = method->isStatic(); |
| 48 | + m.is_virtual = method->isVirtualAsWritten(); |
| 49 | + m.access = AccessSpecifierToString(method->getAccess()); |
| 50 | + m.comment = GetComment(method, *context_); |
| 51 | + |
| 52 | + SourceLocation src_loc = method->getLocation(); |
| 53 | + PresumedLoc presu_loc = src_manager.getPresumedLoc(src_loc); |
| 54 | + if (!presu_loc.isInvalid()) { |
| 55 | + m.location.file = presu_loc.getFilename(); |
| 56 | + m.location.line = presu_loc.getLine(); |
| 57 | + m.location.column = presu_loc.getColumn(); |
| 58 | + } |
| 59 | + |
| 60 | + for (auto param : method->parameters()) { |
| 61 | + ParameterInfo p; |
| 62 | + p.name = param->getNameAsString(); |
| 63 | + p.type = param->getOriginalType().getAsString(); |
| 64 | + m.parameters.emplace_back(p); |
| 65 | + } |
| 66 | + cls.methods.emplace_back(m); |
| 67 | + } |
| 68 | + } |
| 69 | + classes_.emplace_back(cls); |
| 70 | + } |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +bool ASTVisitor::VisitFunctionDecl(const FunctionDecl* func_decl) { |
| 75 | + if (isa<CXXMethodDecl>(func_decl)) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if (!func_decl->isThisDeclarationADefinition()) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + const SourceManager& src_manager = context_->getSourceManager(); |
| 82 | + if (!IsProjectFile(src_manager, func_decl->getLocation())) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + FunctionInfo func; |
| 87 | + func.name = func_decl->getNameAsString(); |
| 88 | + func.return_type = func_decl->getReturnType().getAsString(); |
| 89 | + func.is_inline = func_decl->isInlineSpecified(); |
| 90 | + func.comment = GetComment(func_decl, *context_); |
| 91 | + func.ns = GetEnclosingNamespace(func_decl); |
| 92 | + |
| 93 | + SourceLocation loc = func_decl->getLocation(); |
| 94 | + PresumedLoc presu_loc = src_manager.getPresumedLoc(loc); |
| 95 | + if (!presu_loc.isInvalid()) { |
| 96 | + func.location.file = presu_loc.getFilename(); |
| 97 | + func.location.line = presu_loc.getLine(); |
| 98 | + func.location.column = presu_loc.getColumn(); |
| 99 | + } |
| 100 | + |
| 101 | + for (auto param : func_decl->parameters()) { |
| 102 | + ParameterInfo p; |
| 103 | + p.name = param->getNameAsString(); |
| 104 | + p.type = param->getOriginalType().getAsString(); |
| 105 | + func.parameters.emplace_back(p); |
| 106 | + } |
| 107 | + |
| 108 | + functions_.emplace_back(func); |
| 109 | + return true; |
| 110 | +} |
| 111 | + |
| 112 | +const std::vector<ClassInfo>& ASTVisitor::classes() const { |
| 113 | + return classes_; |
| 114 | +} |
| 115 | + |
| 116 | +const std::vector<FunctionInfo>& ASTVisitor::functions() const { |
| 117 | + return functions_; |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace tooling |
| 121 | +} // namespace clang |
0 commit comments