Skip to content

Commit

Permalink
Added nested transparent blocks to handle case and default labels exp…
Browse files Browse the repository at this point in the history
…licitly.
  • Loading branch information
dbukki committed Feb 28, 2024
1 parent 4d2104e commit c78193a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 4 additions & 4 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
scope = std::make_unique<NestedOneWayScope>(&_scopeStack, cs, cs->getHandlerBlock());
else if (clang::CompoundStmt* cs = llvm::dyn_cast<clang::CompoundStmt>(s_))
scope = std::make_unique<NestedCompoundScope>(&_scopeStack, cs);
else if (!llvm::isa<clang::SwitchCase>(s_))
else if (clang::SwitchCase* sc = llvm::dyn_cast<clang::SwitchCase>(s_))
scope = std::make_unique<NestedTransparentScope>(&_scopeStack, sc);
else
// This is why we can't do this in the indidvidual handler functions:
// There is no Traverse* function equivalent to a general 'else' case
// when the current statement is neither of the above specified ones.
Expand All @@ -489,9 +491,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
// statement would essentially be duplicated on the scope stack,
// which would disrupt the meaning of actual scopes in the code.
scope = std::make_unique<NestedStatementScope>(&_scopeStack, s_);

if (scope)
CountBumpiness(*scope);
CountBumpiness(*scope);

return Base::TraverseStmt(s_);
}
Expand Down
18 changes: 13 additions & 5 deletions plugins/cpp/parser/src/nestedscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,29 @@ namespace parser
}


NestedScope::State NestedCompoundScope::CheckNext(clang::Stmt*) const
NestedScope::State NestedTransparentScope::CheckNext(clang::Stmt*) const
{
// Anything inside a compound statement block is standalone,
// Anything inside a transparent statement block is standalone,
// we do not expect any particular type of statement to be nested in it.
return State::Standalone;
}

NestedCompoundScope::NestedCompoundScope(
NestedTransparentScope::NestedTransparentScope(
NestedStack* stack_,
clang::Stmt* stmt_
) :
NestedScope(stack_, stmt_)
{}


NestedCompoundScope::NestedCompoundScope(
NestedStack* stack_,
clang::Stmt* stmt_
) :
NestedTransparentScope(stack_, stmt_)
{
// A compound statement block only counts as a real scope when it is
// not the expected body of any other statement (e.g. if, while, ...)
// A compound statement block only counts as a non-transparent scope
// when it is not the expected body of any other statement.
if (_state == State::Standalone)
++_depth;
}
Expand Down
8 changes: 7 additions & 1 deletion plugins/cpp/parser/src/nestedscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ namespace parser
virtual ~NestedScope();
};

class NestedCompoundScope : public NestedScope
class NestedTransparentScope : public NestedScope
{
protected:
virtual State CheckNext(clang::Stmt* stmt_) const override;

public:
NestedTransparentScope(NestedStack* stack_, clang::Stmt* stmt_);
};

class NestedCompoundScope : public NestedTransparentScope
{
public:
NestedCompoundScope(NestedStack* stack_, clang::Stmt* stmt_);
};
Expand Down

0 comments on commit c78193a

Please sign in to comment.