[clang-tools-extra] r225519 - clang-tidy: [misc-use-override] Fix 'override' insertion.

Daniel Jasper djasper at google.com
Fri Jan 9 05:56:36 PST 2015


Author: djasper
Date: Fri Jan  9 07:56:35 2015
New Revision: 225519

URL: http://llvm.org/viewvc/llvm-project?rev=225519&view=rev
Log:
clang-tidy: [misc-use-override] Fix 'override' insertion.

Before:
  void f() __attribute__((override unused))

After:
  void f() override __attribute__((unused))

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/UseOverride.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-use-override.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/UseOverride.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UseOverride.cpp?rev=225519&r1=225518&r2=225519&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/UseOverride.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UseOverride.cpp Fri Jan  9 07:56:35 2015
@@ -23,15 +23,16 @@ void UseOverride::registerMatchers(Match
 
 // Re-lex the tokens to get precise locations to insert 'override' and remove
 // 'virtual'.
-static SmallVector<Token, 16> ParseTokens(CharSourceRange Range,
-                                          const SourceManager &Sources,
-                                          LangOptions LangOpts) {
+static SmallVector<Token, 16>
+ParseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
+  const SourceManager &Sources = *Result.SourceManager;
   std::pair<FileID, unsigned> LocInfo =
       Sources.getDecomposedLoc(Range.getBegin());
   StringRef File = Sources.getBufferData(LocInfo.first);
   const char *TokenBegin = File.data() + LocInfo.second;
-  Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first), LangOpts,
-                 File.begin(), TokenBegin, File.end());
+  Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
+                 Result.Context->getLangOpts(), File.begin(), TokenBegin,
+                 File.end());
   SmallVector<Token, 16> Tokens;
   Token Tok;
   while (!RawLexer.LexFromRawLexer(Tok)) {
@@ -39,6 +40,12 @@ static SmallVector<Token, 16> ParseToken
       break;
     if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
       break;
+    if (Tok.is(tok::raw_identifier)) {
+      IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
+          Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
+      Tok.setIdentifierInfo(&Info);
+      Tok.setKind(Info.getTokenID());
+    }
     Tokens.push_back(Tok);
   }
   return Tokens;
@@ -87,18 +94,25 @@ void UseOverride::check(const MatchFinde
   // FIXME: Instead of re-lexing and looking for specific macros such as
   // 'ABSTRACT', properly store the location of 'virtual' and '= 0' in each
   // FunctionDecl.
-  SmallVector<Token, 16> Tokens = ParseTokens(FileRange, Sources,
-                                              Result.Context->getLangOpts());
+  SmallVector<Token, 16> Tokens = ParseTokens(FileRange, Result);
 
   // Add 'override' on inline declarations that don't already have it.
   if (!HasFinal && !HasOverride) {
     SourceLocation InsertLoc;
     StringRef ReplacementText = "override ";
 
+    for (Token T : Tokens) {
+      if (T.is(tok::kw___attribute)) {
+        InsertLoc = T.getLocation();
+        break;
+      }
+    }
+
     if (Method->hasAttrs()) {
       for (const clang::Attr *A : Method->getAttrs()) {
         if (!A->isImplicit()) {
-          SourceLocation Loc = Sources.getExpansionLoc(A->getLocation());
+          SourceLocation Loc =
+              Sources.getExpansionLoc(A->getRange().getBegin());
           if (!InsertLoc.isValid() ||
               Sources.isBeforeInTranslationUnit(Loc, InsertLoc))
             InsertLoc = Loc;
@@ -134,7 +148,7 @@ void UseOverride::check(const MatchFinde
 
   if (Method->isVirtualAsWritten()) {
     for (Token Tok : Tokens) {
-      if (Tok.is(tok::raw_identifier) && GetText(Tok, Sources) == "virtual") {
+      if (Tok.is(tok::kw_virtual)) {
         Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
             Tok.getLocation(), Tok.getLocation()));
         break;

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-use-override.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-use-override.cpp?rev=225519&r1=225518&r2=225519&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-use-override.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-use-override.cpp Fri Jan  9 07:56:35 2015
@@ -29,6 +29,7 @@ struct Base {
   virtual bool n() MUST_USE_RESULT UNUSED;
 
   virtual void m();
+  virtual void o() __attribute__((unused));
 };
 
 struct SimpleCases : public Base {
@@ -84,6 +85,10 @@ public:
   virtual void m() override final;
   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: Annotate this
   // CHECK-FIXES: {{^  void m\(\) final;}}
+
+  virtual void o() __attribute__((unused));
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: Prefer using
+  // CHECK-FIXES: {{^  void o\(\) override __attribute__\(\(unused\)\);}}
 };
 
 // CHECK-MESSAGES-NOT: warning:





More information about the cfe-commits mailing list