22 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
33 */
44
5- #include " StmtBordersFinder .h"
5+ #include " BordersFinder .h"
66
77#include " Paths.h"
88#include " clang-utils/ASTPrinter.h"
@@ -17,23 +17,39 @@ using namespace llvm;
1717using namespace clang ::ast_matchers;
1818using namespace clang ::tooling;
1919
20- StmtBordersFinder::StmtBordersFinder (const fs::path &filePath,
21- unsigned line,
22- const shared_ptr<CompilationDatabase> &compilationDatabase,
23- const fs::path &compileCommandsJsonPath)
24- : line(line), clangToolRunner(compilationDatabase) {
20+ BordersFinder::BordersFinder (const fs::path &filePath,
21+ unsigned line,
22+ const shared_ptr<CompilationDatabase> &compilationDatabase,
23+ const fs::path &compileCommandsJsonPath)
24+ : line(line), classBorder(std:: nullopt ), clangToolRunner(compilationDatabase) {
2525 buildRootPath = Paths::subtractPath (compileCommandsJsonPath.string (), CompilationUtils::MOUNTED_CC_JSON_DIR_NAME);
2626 lineInfo.filePath = filePath;
2727}
2828
29- void StmtBordersFinder ::run (const MatchFinder::MatchResult &Result) {
29+ void BordersFinder ::run (const MatchFinder::MatchResult &Result) {
3030 LOG_SCOPE_FUNCTION (MAX);
31- if (const auto *FS = Result.Nodes .getNodeAs <FunctionDecl>(Matchers::FUNCTION_DEF)) {
31+ if (const auto *ST = Result.Nodes .getNodeAs <clang::CXXRecordDecl>(Matchers::STRUCT_OR_CLASS_JUST_DECL)) {
32+ SourceManager &sourceManager = Result.Context ->getSourceManager ();
33+ fs::path path = sourceManager.getFileEntryForID (sourceManager.getMainFileID ())
34+ ->tryGetRealPathName ()
35+ .str ();
36+ auto borders = getBorders (sourceManager, ST->getSourceRange ());
37+ if (!containsLine (borders) || (classBorder.has_value () && !(borders < classBorder.value ()))) {
38+ return ;
39+ }
40+ classBorder = borders;
41+ lineInfo.begin = borders.start .line ;
42+ lineInfo.end = borders.end .line ;
43+ lineInfo.scopeName = ST->getNameAsString ();
44+ lineInfo.initialized = true ;
45+ LOG_S (MAX) << " Class name: " << ST->getNameAsString ();
46+ LOG_S (MAX) << " Class's borders: " << lineInfo.begin << ' ' << lineInfo.end ;
47+ } else if (const auto *FS = Result.Nodes .getNodeAs <FunctionDecl>(Matchers::FUNCTION_DEF)) {
3248 SourceManager &sourceManager = Result.Context ->getSourceManager ();
3349
3450 fs::path path = sourceManager.getFileEntryForID (sourceManager.getMainFileID ())
35- ->tryGetRealPathName ()
36- .str ();
51+ ->tryGetRealPathName ()
52+ .str ();
3753 Stmt *currentStmt = FS->getBody ();
3854 if ((currentStmt == nullptr ) || !containsLine (getFunctionBordersLines (sourceManager, FS))) {
3955 return ;
@@ -51,24 +67,24 @@ void StmtBordersFinder::run(const MatchFinder::MatchResult &Result) {
5167 hasInnerChild = true ;
5268 if (isa<IfStmt>(currentStmt) || isa<ForStmt>(currentStmt) ||
5369 isa<WhileStmt>(currentStmt)) {
54- if (line == borders.first ) {
70+ if (line == borders.start . line ) {
5571 hasInnerChild = false ;
5672 } else {
5773 lineInfo.wrapInBrackets = true ;
5874 lineInfo.insertAfter = false ;
5975 }
6076 }
61- if (line == borders.first && isa<ReturnStmt>(currentStmt)) {
77+ if (line == borders.start . line && isa<ReturnStmt>(currentStmt)) {
6278 lineInfo.insertAfter = false ;
6379 }
6480 break ;
6581 }
6682 }
6783 }
68- auto *nodeParent = (CXXRecordDecl *)FS->getParent ();
84+ auto *nodeParent = (CXXRecordDecl *) FS->getParent ();
6985 auto borders = getStmtBordersLines (sourceManager, currentStmt);
70- lineInfo.begin = borders.first ;
71- lineInfo.end = borders.second ;
86+ lineInfo.begin = borders.start . line ;
87+ lineInfo.end = borders.end . line ;
7288 lineInfo.scopeName = nodeParent != nullptr ? nodeParent->getNameAsString () : path.stem ().string ();
7389 lineInfo.methodName = FS->getNameAsString ();
7490 const clang::QualType realReturnType = FS->getReturnType ().getCanonicalType ();
@@ -84,8 +100,8 @@ void StmtBordersFinder::run(const MatchFinder::MatchResult &Result) {
84100 break ;
85101 }
86102 auto tempBorders = getStmtBordersLinesDynamic (sourceManager, parents[0 ]);
87- int from = tempBorders.first ;
88- int to = tempBorders.second ;
103+ int from = tempBorders.start . line ;
104+ int to = tempBorders.end . line ;
89105 if (to - from > 1 ) {
90106 break ;
91107 }
@@ -94,45 +110,52 @@ void StmtBordersFinder::run(const MatchFinder::MatchResult &Result) {
94110 }
95111 lineInfo.stmtString = strRepresentation;
96112 LOG_S (MAX) << " Method name: " << lineInfo.methodName << " \n "
97- << " Method's borders: " << borders.first << ' ' << borders.second ;
113+ << " Method's borders: " << borders.start . line << ' ' << borders.end . line ;
98114 LOG_S (DEBUG) << " Statement string: " << lineInfo.stmtString ;
99115 }
100116}
101117
102- LineInfo StmtBordersFinder ::getLineInfo () {
118+ LineInfo BordersFinder ::getLineInfo () {
103119 return lineInfo;
104120}
105121
106- std::pair< unsigned , unsigned > StmtBordersFinder ::getFunctionBordersLines (const SourceManager &srcMng, const FunctionDecl *FS) {
122+ BordersFinder::Borders BordersFinder ::getFunctionBordersLines (const SourceManager &srcMng, const FunctionDecl *FS) {
107123 auto currentStmt = FS->getBody ();
108- auto bodyBorders = getStmtBordersLines (srcMng, currentStmt);
109- auto declBorders = getStmtBordersLines (srcMng, FS->getSourceRange ());
110- return {declBorders.first , bodyBorders.second };
124+ return getStmtBordersLines (srcMng, currentStmt);
111125}
112126
113- std::pair< unsigned , unsigned > StmtBordersFinder ::getStmtBordersLines (const SourceManager &srcMng, const Stmt *st) {
127+ BordersFinder::Borders BordersFinder ::getStmtBordersLines (const SourceManager &srcMng, const Stmt *st) {
114128 return getStmtBordersLinesDynamic (srcMng, clang::ast_type_traits::DynTypedNode::create (*st));
115129}
116130
117- std::pair< unsigned , unsigned > StmtBordersFinder ::getStmtBordersLinesDynamic (const SourceManager &srcMng,
118- const clang::ast_type_traits::DynTypedNode st) {
131+ BordersFinder::Borders BordersFinder ::getStmtBordersLinesDynamic (const SourceManager &srcMng,
132+ const clang::ast_type_traits::DynTypedNode st) {
119133 auto sourceRange = st.getSourceRange ();
120- return getStmtBordersLines (srcMng, sourceRange);
134+ return getBorders (srcMng, sourceRange);
121135}
122136
123- std::pair<unsigned int , unsigned int >
124- StmtBordersFinder::getStmtBordersLines (const SourceManager &srcMng, const SourceRange &sourceRange) {
137+ BordersFinder::Borders BordersFinder::getBorders (const SourceManager &srcMng, const SourceRange &sourceRange) {
125138 auto beginLine = srcMng.getExpansionLineNumber (sourceRange.getBegin ());
126139 auto endLine = srcMng.getExpansionLineNumber (sourceRange.getEnd ());
127- return { beginLine, endLine };
140+ auto beginColumn = srcMng.getExpansionColumnNumber (sourceRange.getBegin ());
141+ auto endColumn = srcMng.getExpansionColumnNumber (sourceRange.getEnd ());
142+ return {{beginLine, beginColumn},
143+ {endLine, endColumn}};
128144}
129145
130- bool StmtBordersFinder ::containsLine (std::pair< unsigned , unsigned > b) const {
131- return line >= b.first && line <= b.second ;
146+ bool BordersFinder ::containsLine (BordersFinder::Borders b) const {
147+ return line >= b.start . line && line <= b.end . line ;
132148}
133149
134- void StmtBordersFinder::launch () {
150+ void BordersFinder::findFunction () {
135151 MatchFinder finder;
136152 finder.addMatcher (Matchers::functionDefinitionMatcher, this );
137153 clangToolRunner.run (lineInfo.filePath , newFrontendActionFactory (&finder).get ());
138154}
155+
156+ void BordersFinder::findClass () {
157+ MatchFinder finder;
158+ finder.addMatcher (Matchers::classJustDeclMatcher, this );
159+ finder.addMatcher (Matchers::structJustDeclMatcher, this );
160+ clangToolRunner.run (lineInfo.filePath , newFrontendActionFactory (&finder).get (), false , std::nullopt , false );
161+ }
0 commit comments