summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorSamuel Benzaquen <sbenza@google.com>2016-06-01 20:37:23 +0000
committerSamuel Benzaquen <sbenza@google.com>2016-06-01 20:37:23 +0000
commitaa05ae91fb7cce0cb45c2fdf74708e07bcf4cfb2 (patch)
tree3e638e93c8dc3a4b5ce02470c6bb8fdbaee582db /clang-tools-extra
parent5573483c5bfff1fe65816efa34941e62998aefdb (diff)
downloadbcm5719-llvm-aa05ae91fb7cce0cb45c2fdf74708e07bcf4cfb2.tar.gz
bcm5719-llvm-aa05ae91fb7cce0cb45c2fdf74708e07bcf4cfb2.zip
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. llvm-svn: 271426
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp13
-rw-r--r--clang-tools-extra/test/clang-tidy/readability-avoid-const-params-in-decls.cpp7
2 files changed, 15 insertions, 5 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
index 8a2a053c4ae..7a19a678f32 100644
--- a/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ b/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
@@ -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::registerMatchers(MatchFinder *Finder) {
}
// 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 Range,
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(const MatchFinder::MatchResult &Result) {
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
diff --git a/clang-tools-extra/test/clang-tidy/readability-avoid-const-params-in-decls.cpp b/clang-tools-extra/test/clang-tidy/readability-avoid-const-params-in-decls.cpp
index aa70700c95b..ef0f4bdded8 100644
--- a/clang-tools-extra/test/clang-tidy/readability-avoid-const-params-in-decls.cpp
+++ b/clang-tools-extra/test/clang-tidy/readability-avoid-const-params-in-decls.cpp
@@ -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);
OpenPOWER on IntegriCloud