[clang-tools-extra] r271426 - Fix uninitialized memory access when the token 'const' is not present in

Samuel Benzaquen via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 1 13:37:24 PDT 2016


Author: sbenza
Date: Wed Jun  1 15:37:23 2016
New Revision: 271426

URL: http://llvm.org/viewvc/llvm-project?rev=271426&view=rev
Log:
Fix uninitialized memory access when the token 'const' is not present in
the program.

If the token is not there, we still warn but we don't try to give a
fixit hint.

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp?rev=271426&r1=271425&r2=271426&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/AvoidConstParamsInDecls.cpp Wed Jun  1 15:37:23 2016
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "AvoidConstParamsInDecls.h"
+#include "llvm/ADT/Optional.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Lex/Lexer.h"
@@ -38,8 +39,8 @@ void AvoidConstParamsInDecls::registerMa
 }
 
 // Re-lex the tokens to get precise location of last 'const'
-static Token ConstTok(CharSourceRange Range,
-                      const MatchFinder::MatchResult &Result) {
+static llvm::Optional<Token> ConstTok(CharSourceRange Range,
+                                      const MatchFinder::MatchResult &Result) {
   const SourceManager &Sources = *Result.SourceManager;
   std::pair<FileID, unsigned> LocInfo =
       Sources.getDecomposedLoc(Range.getBegin());
@@ -49,7 +50,7 @@ static Token ConstTok(CharSourceRange Ra
                  Result.Context->getLangOpts(), File.begin(), TokenBegin,
                  File.end());
   Token Tok;
-  Token ConstTok;
+  llvm::Optional<Token> ConstTok;
   while (!RawLexer.LexFromRawLexer(Tok)) {
     if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
       break;
@@ -94,9 +95,11 @@ void AvoidConstParamsInDecls::check(cons
   if (!FileRange.isValid())
     return;
 
-  Token Tok = ConstTok(FileRange, Result);
+  auto Tok = ConstTok(FileRange, Result);
+  if (!Tok)
+    return;
   Diag << FixItHint::CreateRemoval(
-      CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getLocation()));
+      CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
 }
 
 } // namespace readability

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp?rev=271426&r1=271425&r2=271426&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-avoid-const-params-in-decls.cpp Wed Jun  1 15:37:23 2016
@@ -83,3 +83,10 @@ void NF(const int&);
 void NF(const int*);
 void NF(const int* const*);
 void NF(alias_const_type);
+
+// Regression test for when the 'const' token is not in the code.
+#define CONCAT(a, b) a##b
+void ConstNotVisible(CONCAT(cons, t) int i);
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: parameter 'i'
+// We warn, but we can't give a fix
+// CHECK-FIXES: void ConstNotVisible(CONCAT(cons, t) int i);




More information about the cfe-commits mailing list