Kaydet (Commit) 597178ce authored tarafından Luboš Luňák's avatar Luboš Luňák

simplify bodynotinblock plugin using parentStmt()

Change-Id: Ia2fe10af2ca555c7b88348e7ed571f1176586580
üst 81b58bb0
...@@ -38,50 +38,42 @@ void BodyNotInBlock::run() ...@@ -38,50 +38,42 @@ void BodyNotInBlock::run()
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
} }
bool BodyNotInBlock::VisitFunctionDecl( const FunctionDecl* declaration ) bool BodyNotInBlock::VisitIfStmt( const IfStmt* stmt )
{ {
if( ignoreLocation( declaration )) if( ignoreLocation( stmt ))
return true; return true;
if( !declaration->doesThisDeclarationHaveABody()) checkBody( stmt->getThen(), stmt->getIfLoc(), 0, stmt->getElse() != NULL );
checkBody( stmt->getElse(), stmt->getElseLoc(), 0 );
return true;
}
bool BodyNotInBlock::VisitWhileStmt( const WhileStmt* stmt )
{
if( ignoreLocation( stmt ))
return true; return true;
StmtParents parents; checkBody( stmt->getBody(), stmt->getWhileLoc(), 1 );
traverseStatement( declaration->getBody(), parents );
return true; return true;
} }
void BodyNotInBlock::traverseStatement( const Stmt* stmt, StmtParents& parents ) bool BodyNotInBlock::VisitForStmt( const ForStmt* stmt )
{ {
parents.push_back( stmt ); if( ignoreLocation( stmt ))
for( ConstStmtIterator it = stmt->child_begin(); return true;
it != stmt->child_end(); checkBody( stmt->getBody(), stmt->getForLoc(), 2 );
++it ) return true;
{ }
if( *it != NULL ) // some children can be apparently NULL
{ bool BodyNotInBlock::VisitCXXForRangeStmt( const CXXForRangeStmt* stmt )
traverseStatement( *it, parents ); // substatements first {
parents.push_back( *it ); if( ignoreLocation( stmt ))
if( const IfStmt* ifstmt = dyn_cast< IfStmt >( *it )) return true;
{ checkBody( stmt->getBody(), stmt->getForLoc(), 2 );
checkBody( ifstmt->getThen(), ifstmt->getIfLoc(), parents, 0, ifstmt->getElse() != NULL ); return true;
checkBody( ifstmt->getElse(), ifstmt->getElseLoc(), parents, 0 );
}
else if( const WhileStmt* whilestmt = dyn_cast< WhileStmt >( *it ))
checkBody( whilestmt->getBody(), whilestmt->getWhileLoc(), parents, 1 );
else if( const ForStmt* forstmt = dyn_cast< ForStmt >( *it ))
checkBody( forstmt->getBody(), forstmt->getForLoc(), parents, 2 );
else if( const CXXForRangeStmt* forstmt = dyn_cast< CXXForRangeStmt >( *it ))
checkBody( forstmt->getBody(), forstmt->getForLoc(), parents, 2 );
parents.pop_back();
}
}
assert( parents.back() == stmt );
parents.pop_back();
} }
void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, const StmtParents& parents, void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, int stmtType, bool dontGoUp )
int stmtType, bool dontGoUp )
{ {
if( body == NULL || parents.size() < 2 ) if( body == NULL )
return; return;
// TODO: If the if/else/while/for comes from a macro expansion, ignore it completely for // TODO: If the if/else/while/for comes from a macro expansion, ignore it completely for
// now. The code below could assume everything is in the same place (and thus also column) // now. The code below could assume everything is in the same place (and thus also column)
...@@ -92,22 +84,24 @@ void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, c ...@@ -92,22 +84,24 @@ void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, c
return; return;
if( dyn_cast< CompoundStmt >( body )) if( dyn_cast< CompoundStmt >( body ))
return; // if body is a compound statement, then it is in {} return; // if body is a compound statement, then it is in {}
const Stmt* previousParent = parentStmt( body ); // Here the statement itself.
// Find the next statement (in source position) after 'body'. // Find the next statement (in source position) after 'body'.
for( int parent_pos = parents.size() - 2; // start from grandparent for(;;)
parent_pos >= 0;
--parent_pos )
{ {
for( ConstStmtIterator it = parents[ parent_pos ]->child_begin(); const Stmt* parent = parentStmt( previousParent );
it != parents[ parent_pos ]->child_end(); if( parent == NULL )
break;
for( ConstStmtIterator it = parent->child_begin();
it != parent->child_end();
) )
{ {
if( *it == parents[ parent_pos + 1 ] ) // found grand(grand...)parent if( *it == previousParent ) // found grand(grand...)parent
{ {
// get next statement after our (grand...)parent // get next statement after our (grand...)parent
++it; ++it;
while( it != parents[ parent_pos ]->child_end() && *it == NULL ) while( it != parent->child_end() && *it == NULL )
++it; // skip empty ones (missing 'else' bodies for example) ++it; // skip empty ones (missing 'else' bodies for example)
if( it != parents[ parent_pos ]->child_end()) if( it != parent->child_end())
{ {
bool invalid1, invalid2; bool invalid1, invalid2;
unsigned bodyColumn = compiler.getSourceManager() unsigned bodyColumn = compiler.getSourceManager()
...@@ -134,13 +128,14 @@ void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, c ...@@ -134,13 +128,14 @@ void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, c
} }
// If going up would mean leaving a {} block, stop, because the } should // If going up would mean leaving a {} block, stop, because the } should
// make it visible the two statements are not in the same body. // make it visible the two statements are not in the same body.
if( dyn_cast< CompoundStmt >( parents[ parent_pos ] )) if( dyn_cast< CompoundStmt >( parent ))
return; return;
// If the body to be checked is a body of an if statement that has also // If the body to be checked is a body of an if statement that has also
// an else part, don't go up, the else is after the body and should make // an else part, don't go up, the else is after the body and should make
// it clear the body does not continue there. // it clear the body does not continue there.
if( dontGoUp ) if( dontGoUp )
return; return;
previousParent = parent;
} }
} }
......
...@@ -23,12 +23,13 @@ class BodyNotInBlock ...@@ -23,12 +23,13 @@ class BodyNotInBlock
public: public:
explicit BodyNotInBlock( CompilerInstance& compiler ); explicit BodyNotInBlock( CompilerInstance& compiler );
virtual void run() override; virtual void run() override;
bool VisitFunctionDecl( const FunctionDecl* declaration ); bool VisitIfStmt( const IfStmt* stmt );
bool VisitWhileStmt( const WhileStmt* stmt );
bool VisitForStmt( const ForStmt* stmt );
bool VisitCXXForRangeStmt( const CXXForRangeStmt* stmt );
private: private:
typedef vector< const Stmt* > StmtParents; typedef vector< const Stmt* > StmtParents;
void traverseStatement( const Stmt* stmt, StmtParents& parents ); void checkBody( const Stmt* body, SourceLocation stmtLocation, int stmtType, bool dontGoUp = false );
void checkBody( const Stmt* body, SourceLocation stmtLocation, const StmtParents& parents,
int stmtType, bool dontGoUp = false );
}; };
} // namespace } // namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment