]> granicus.if.org Git - clang/commitdiff
Fix BackendConsumer::EmitOptimizationMessage()
authorDiego Novillo <dnovillo@google.com>
Fri, 8 May 2015 20:59:56 +0000 (20:59 +0000)
committerDiego Novillo <dnovillo@google.com>
Fri, 8 May 2015 20:59:56 +0000 (20:59 +0000)
Patch from Geoff Berry <gberry@codeaurora.org>

Fix BackendConsumer::EmitOptimizationMessage() to check if the
DiagnosticInfoOptimizationBase object has a valid location before
calling getLocation() to avoid dereferencing a null pointer inside
getLocation() when no debug info is present.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236898 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenAction.cpp
test/Misc/backend-optimization-failure-nodbg.cpp [new file with mode: 0644]

index b06fe424fe0379c94b4520eccefd182f71dca393..075832f076556f005952ce0677321a2525744209 100644 (file)
@@ -434,13 +434,16 @@ void BackendConsumer::EmitOptimizationMessage(
   FileManager &FileMgr = SourceMgr.getFileManager();
   StringRef Filename;
   unsigned Line, Column;
-  D.getLocation(&Filename, &Line, &Column);
   SourceLocation DILoc;
-  const FileEntry *FE = FileMgr.getFile(Filename);
-  if (FE && Line > 0) {
-    // If -gcolumn-info was not used, Column will be 0. This upsets the
-    // source manager, so pass 1 if Column is not set.
-    DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
+
+  if (D.isLocationAvailable()) {
+    D.getLocation(&Filename, &Line, &Column);
+    const FileEntry *FE = FileMgr.getFile(Filename);
+    if (FE && Line > 0) {
+      // If -gcolumn-info was not used, Column will be 0. This upsets the
+      // source manager, so pass 1 if Column is not set.
+      DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
+    }
   }
 
   // If a location isn't available, try to approximate it using the associated
@@ -455,7 +458,7 @@ void BackendConsumer::EmitOptimizationMessage(
       << AddFlagValue(D.getPassName() ? D.getPassName() : "")
       << D.getMsg().str();
 
-  if (DILoc.isInvalid())
+  if (DILoc.isInvalid() && D.isLocationAvailable())
     // If we were not able to translate the file:line:col information
     // back to a SourceLocation, at least emit a note stating that
     // we could not translate this location. This can happen in the
diff --git a/test/Misc/backend-optimization-failure-nodbg.cpp b/test/Misc/backend-optimization-failure-nodbg.cpp
new file mode 100644 (file)
index 0000000..3c32646
--- /dev/null
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -O3 -emit-llvm -S -verify -o /dev/null
+// REQUIRES: x86-registered-target
+
+// Test verifies optimization failures generated by the backend are handled
+// correctly by clang. LLVM tests verify all of the failure conditions.
+
+void test_switch(int *A, int *B, int Length) {
+#pragma clang loop vectorize(enable) unroll(disable)
+  for (int i = 0; i < Length; i++) {
+    switch (A[i]) {
+    case 0:
+      B[i] = 1;
+      break;
+    case 1:
+      B[i] = 2;
+      break;
+    default:
+      B[i] = 3;
+    }
+  }
+/* expected-warning {{loop not vectorized: failed explicitly specified loop vectorization}} */ }