summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevin Coughlin <dcoughlin@apple.com>2016-08-30 23:07:14 +0000
committerDevin Coughlin <dcoughlin@apple.com>2016-08-30 23:07:14 +0000
commit184996bbb4b82ad947507365e55afc5bcca4836d (patch)
tree27ea95274227da55f6ca6dc1a27c6f1a9b5e994e
parentf6275a08ef01f39b7a7c788657c7e1c34013fe9e (diff)
downloadbcm5719-llvm-184996bbb4b82ad947507365e55afc5bcca4836d.tar.gz
bcm5719-llvm-184996bbb4b82ad947507365e55afc5bcca4836d.zip
[analyzer] Use lazily created buffer in EmptyLocalizationContextChecker
Fix a crash when relexing the underlying memory buffer to find incorrect arguments to NSLocalizedString(). With precompiled headers, the raw buffer may be NULL. Instead, use the source manager to get the buffer, which will lazily create the buffer for precompiled headers. rdar://problem/27429091 llvm-svn: 280174
-rw-r--r--clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp9
-rw-r--r--clang/test/Analysis/Inputs/localization-pch.h5
-rw-r--r--clang/test/Analysis/localization-aggressive.m8
3 files changed, 20 insertions, 2 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
index 1386f97cda5..d1dab6d27d4 100644
--- a/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
@@ -1016,6 +1016,8 @@ void EmptyLocalizationContextChecker::checkASTDecl(
void EmptyLocalizationContextChecker::MethodCrawler::VisitObjCMessageExpr(
const ObjCMessageExpr *ME) {
+ // FIXME: We may be able to use PPCallbacks to check for empy context
+ // comments as part of preprocessing and avoid this re-lexing hack.
const ObjCInterfaceDecl *OD = ME->getReceiverInterface();
if (!OD)
return;
@@ -1050,7 +1052,12 @@ void EmptyLocalizationContextChecker::MethodCrawler::VisitObjCMessageExpr(
SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
}
- llvm::MemoryBuffer *BF = SE.getFile().getContentCache()->getRawBuffer();
+ bool Invalid = false;
+ llvm::MemoryBuffer *BF =
+ Mgr.getSourceManager().getBuffer(SLInfo.first, SL, &Invalid);
+ if (Invalid)
+ return;
+
Lexer TheLexer(SL, LangOptions(), BF->getBufferStart(),
BF->getBufferStart() + SLInfo.second, BF->getBufferEnd());
diff --git a/clang/test/Analysis/Inputs/localization-pch.h b/clang/test/Analysis/Inputs/localization-pch.h
new file mode 100644
index 00000000000..973270e24ef
--- /dev/null
+++ b/clang/test/Analysis/Inputs/localization-pch.h
@@ -0,0 +1,5 @@
+// Used to test missing checker for missing localization context comments
+// in precompiled headers.
+
+#define MyLocalizedStringInPCH(key) NSLocalizedString((key), @"")
+
diff --git a/clang/test/Analysis/localization-aggressive.m b/clang/test/Analysis/localization-aggressive.m
index 79c9c133121..89950d4eed7 100644
--- a/clang/test/Analysis/localization-aggressive.m
+++ b/clang/test/Analysis/localization-aggressive.m
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -analyze -fblocks -analyzer-store=region -analyzer-checker=optin.osx.cocoa.localizability.NonLocalizedStringChecker -analyzer-checker=optin.osx.cocoa.localizability.EmptyLocalizationContextChecker -verify -analyzer-config AggressiveReport=true %s
+// RUN: %clang_cc1 -fblocks -x objective-c-header -emit-pch -o %t.pch %S/Inputs/localization-pch.h
+
+// RUN: %clang_cc1 -analyze -fblocks -analyzer-store=region -analyzer-checker=optin.osx.cocoa.localizability.NonLocalizedStringChecker -analyzer-checker=optin.osx.cocoa.localizability.EmptyLocalizationContextChecker -include-pch %t.pch -verify -analyzer-config AggressiveReport=true %s
// These declarations were reduced using Delta-Debugging from Foundation.h
// on Mac OS X.
@@ -249,6 +251,10 @@ NSString *ForceLocalized(NSString *str) { return str; }
NSString *string3 = NSLocalizedString((0 ? @"Critical" : @"Current"),nil); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
}
+- (void)testMacroExpansionDefinedInPCH {
+ NSString *string = MyLocalizedStringInPCH(@"Hello"); // expected-warning {{Localized string macro should include a non-empty comment for translators}}
+}
+
#define KCLocalizedString(x,comment) NSLocalizedString(x, comment)
#define POSSIBLE_FALSE_POSITIVE(s,other) KCLocalizedString(s,@"Comment")
OpenPOWER on IntegriCloud