[PATCH] Fix segmentation fault when mixing -Rpass with #line.

Diego Novillo dnovillo at google.com
Wed May 7 09:13:21 PDT 2014


- Check for positive line numbers.

The translation to SourceLocation does not understand line number 0.
That means that the associated instruction did not really have location
information attached to it.

http://reviews.llvm.org/D3625

Files:
  include/clang/Basic/DiagnosticFrontendKinds.td
  lib/CodeGen/CodeGenAction.cpp
  test/Frontend/optimization-remark-line-directive.c

Index: include/clang/Basic/DiagnosticFrontendKinds.td
===================================================================
--- include/clang/Basic/DiagnosticFrontendKinds.td
+++ include/clang/Basic/DiagnosticFrontendKinds.td
@@ -37,6 +37,8 @@
 def note_fe_backend_optimization_remark_missing_loc : Note<"use "
   "-gline-tables-only -gcolumn-info to track source location information "
   "for this optimization remark">;
+def note_fe_backend_optimization_remark_invalid_loc : Note<"could "
+  "not determine the original source location for %0:%1:%2">;
 
 def err_fe_invalid_code_complete_file : Error<
     "cannot locate code-completion file %0">, DefaultFatal;
Index: lib/CodeGen/CodeGenAction.cpp
===================================================================
--- lib/CodeGen/CodeGenAction.cpp
+++ lib/CodeGen/CodeGenAction.cpp
@@ -396,13 +396,13 @@
     unsigned Line, Column;
     D.getLocation(&Filename, &Line, &Column);
     SourceLocation Loc;
-    if (Line > 0) {
+    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 if Column is not set, set it to 1.
       if (Column == 0)
         Column = 1;
-      Loc = SourceMgr.translateFileLineCol(FileMgr.getFile(Filename), Line,
-                                           Column);
+      Loc = SourceMgr.translateFileLineCol(FE, Line, Column);
     }
     Diags.Report(Loc, diag::remark_fe_backend_optimization_remark)
         << AddFlagValue(D.getPassName()) << D.getMsg().str();
@@ -415,6 +415,13 @@
       // -Rpass is used. !srcloc annotations need to be emitted in
       // approximately the same spots as !dbg nodes.
       Diags.Report(diag::note_fe_backend_optimization_remark_missing_loc);
+    else if (Loc.isInvalid())
+      // 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
+      // case of #line directives.
+      Diags.Report(diag::note_fe_backend_optimization_remark_invalid_loc)
+        << Filename << Line << Column;
   }
 }
 
Index: test/Frontend/optimization-remark-line-directive.c
===================================================================
--- /dev/null
+++ test/Frontend/optimization-remark-line-directive.c
@@ -0,0 +1,15 @@
+// This file tests -Rpass diagnostics together with #line
+// directives. We cannot map #line directives back to
+// a SourceLocation.
+
+// RUN: %clang -c %s -Rpass=inline -O0 -S -gmlt -o /dev/null 2> %t.err
+// RUN: FileCheck < %t.err %s --check-prefix=INLINE-INVALID-LOC
+//
+int foo(int x, int y) __attribute__((always_inline));
+int foo(int x, int y) { return x + y; }
+
+#line 1230 "/bad/path/to/original.c"
+int bar(int j) { return foo(j, j - 2); }
+
+// INLINE-INVALID-LOC: {{^remark: foo inlined into bar}}
+// INLINE-INVALID-LOC: note: could not determine the source location for /bad/path/to/original.c:1230:0
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3625.9176.patch
Type: text/x-patch
Size: 3035 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140507/1aef33ee/attachment.bin>


More information about the cfe-commits mailing list