diff options
Diffstat (limited to 'compilerplugins/clang/collapseif.cxx')
-rw-r--r-- | compilerplugins/clang/collapseif.cxx | 30 |
1 files changed, 4 insertions, 26 deletions
diff --git a/compilerplugins/clang/collapseif.cxx b/compilerplugins/clang/collapseif.cxx index b3f192c2ce1d..aecf10f5e0e9 100644 --- a/compilerplugins/clang/collapseif.cxx +++ b/compilerplugins/clang/collapseif.cxx @@ -38,7 +38,6 @@ public: private: int getNoCharsInSourceCodeOfExpr(IfStmt const*); - bool containsComment(Stmt const* stmt); }; bool CollapseIf::VisitIfStmt(IfStmt const* ifStmt) @@ -73,11 +72,11 @@ bool CollapseIf::VisitIfStmt(IfStmt const* ifStmt) // Sometimes there is a comment between the first and second if, so // merging them would make the comment more awkward to write. - if (containsComment(ifStmt)) + if (containsComment(ifStmt->getSourceRange())) return true; report(DiagnosticsEngine::Warning, "nested if should be collapsed into one statement %0 %1", - compat::getBeginLoc(ifStmt)) + ifStmt->getBeginLoc()) << noChars1 << noChars2 << ifStmt->getSourceRange(); return true; } @@ -88,8 +87,8 @@ int CollapseIf::getNoCharsInSourceCodeOfExpr(IfStmt const* ifStmt) // measuring the size of the condition expression is unreliable, because clang // does not report the location of the last token accurately. SourceManager& SM = compiler.getSourceManager(); - SourceLocation startLoc = compat::getBeginLoc(ifStmt); - SourceLocation endLoc = compat::getBeginLoc(ifStmt->getThen()); + SourceLocation startLoc = ifStmt->getBeginLoc(); + SourceLocation endLoc = ifStmt->getThen()->getBeginLoc(); char const* p1 = SM.getCharacterData(startLoc); char const* p2 = SM.getCharacterData(endLoc); @@ -101,27 +100,6 @@ int CollapseIf::getNoCharsInSourceCodeOfExpr(IfStmt const* ifStmt) return count; } -bool CollapseIf::containsComment(Stmt const* stmt) -{ - SourceManager& SM = compiler.getSourceManager(); - auto range = stmt->getSourceRange(); - SourceLocation startLoc = range.getBegin(); - SourceLocation endLoc = range.getEnd(); - char const* p1 = SM.getCharacterData(startLoc); - char const* p2 = SM.getCharacterData(endLoc); - p2 += Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()); - - // check for comments - constexpr char const comment1[] = "/*"; - constexpr char const comment2[] = "//"; - if (std::search(p1, p2, comment1, comment1 + strlen(comment1)) != p2) - return true; - if (std::search(p1, p2, comment2, comment2 + strlen(comment2)) != p2) - return true; - - return false; -} - /** Off by default because some places are a judgement call if it should be collapsed or not. */ loplugin::Plugin::Registration<CollapseIf> X("collapseif", false); } |