r280174 - [analyzer] Use lazily created buffer in EmptyLocalizationContextChecker

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 30 16:07:14 PDT 2016


Author: dcoughlin
Date: Tue Aug 30 18:07:14 2016
New Revision: 280174

URL: http://llvm.org/viewvc/llvm-project?rev=280174&view=rev
Log:
[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

Added:
    cfe/trunk/test/Analysis/Inputs/localization-pch.h
Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
    cfe/trunk/test/Analysis/localization-aggressive.m

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp?rev=280174&r1=280173&r2=280174&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp Tue Aug 30 18:07:14 2016
@@ -1016,6 +1016,8 @@ void EmptyLocalizationContextChecker::ch
 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::Me
     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());
 

Added: cfe/trunk/test/Analysis/Inputs/localization-pch.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/Inputs/localization-pch.h?rev=280174&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/Inputs/localization-pch.h (added)
+++ cfe/trunk/test/Analysis/Inputs/localization-pch.h Tue Aug 30 18:07:14 2016
@@ -0,0 +1,5 @@
+// Used to test missing checker for missing localization context comments
+// in precompiled headers.
+
+#define MyLocalizedStringInPCH(key) NSLocalizedString((key), @"")
+

Modified: cfe/trunk/test/Analysis/localization-aggressive.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/localization-aggressive.m?rev=280174&r1=280173&r2=280174&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/localization-aggressive.m (original)
+++ cfe/trunk/test/Analysis/localization-aggressive.m Tue Aug 30 18:07:14 2016
@@ -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)
   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")
 




More information about the cfe-commits mailing list