[cfe-dev] clang-tidy FixItHint::CreateRemoval -- trailing semicolon stays

Tim Halloran via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 2 10:31:41 PST 2016


Hopefully a simple question. I'm removing a using namespace directive from
the code in a simple check. so

using namespace std;

gets removed from a C++ file my code however ends up with

;

when I run

clang-tidy -checks=-*,surelogic* -fix-errors test1.cpp -- -std=c++14
4 warnings generated.
/home/tim/Source/llvm-work/test1.cpp:8:1: warning: do not use namespace
using-directives; use using-declarations instead
[surelogic-readability-using]
using namespace std;
^
/home/tim/Source/llvm-work/test1.cpp:8:1: note: FIX-IT applied suggested
code changes
using namespace std;
^
<<snip>>
clang-tidy applied 2 of 2 suggested fixes.

The non-boilerplate code is below:

 void UsingCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
    // Only register the matchers for C++; the functionality currently does
not
    // provide any benefit to other languages, despite being benign.
    if (getLangOpts().CPlusPlus)
    Finder->addMatcher(usingDirectiveDecl().bind("usingNamespace"), this);
  }

  void
UsingCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *U =
Result.Nodes.getNodeAs<UsingDirectiveDecl>("usingNamespace");
  SourceLocation Loc = U->getLocStart();
    if (U->isImplicit() || !Loc.isValid())
      return;

    diag(Loc, "do not use namespace using-directives; "
            "use using-declarations instead") <<
FixItHint::CreateRemoval(U->getSourceRange());
  }

(yes this is clone of the google check, just to learn)

But I'm wondering what to do to get the semicolon. I don't see it in the
AST dump output...below

<<snip stuff in my test code>>
|-FunctionDecl 0x482c160 <mod.cpp:5:1, col:37> col:5 used foo 'int (class
Point)'
| |-ParmVarDecl 0x482c098 <col:9, col:15> col:15 used p 'class Point'
| `-CompoundStmt 0x482c2b0 <col:18, col:37>
|   `-ReturnStmt 0x482c298 <col:20, col:34>
|     `-CXXMemberCallExpr 0x482c270 <col:27, col:34> 'int'
|       `-MemberExpr 0x482c238 <col:27, col:29> '<bound member function
type>' .getX 0x482b178
|         `-DeclRefExpr 0x482c210 <col:27> 'class Point' lvalue ParmVar
0x482c098 'p' 'class Point'
<< The using directive is next >>
|-UsingDirectiveDecl 0x482c2d0 <line:8:1, col:17> col:17 Namespace
0x409b938 'std'
<< next a function decl in my test code>>
`-FunctionDecl 0x482c340 <line:10:1, line:41:1> line:10:5 main 'int (void)'
  `-CompoundStmt 0x486d200 <col:12, line:41:1>

Any suggestions?
-- 
Tim Halloran
SureLogic, Inc.
5808 Forbes Avenue, Pittsburgh PA 15217-1602
(412) 722-3338
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160202/cfef8b3c/attachment.html>


More information about the cfe-dev mailing list