Browse Source

Provide fallback locations for backend remarks

Instead of disembodied diagnostics when debug info is disabled it's now
possible to identify the associated function's location in order to provide
some amount of of context.

We use the definition's body right brace location to differentiate the fallback
from diagnostics that genuinely relate to the function declaration itself (a
convention also used by gcc).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210294 91177308-0d34-0410-b5e6-96231b3b80d8
Alp Toker 11 years ago
parent
commit
817255ea36

+ 15 - 8
lib/CodeGen/CodeGenAction.cpp

@@ -424,15 +424,22 @@ void BackendConsumer::EmitOptimizationRemark(
   StringRef Filename;
   StringRef Filename;
   unsigned Line, Column;
   unsigned Line, Column;
   D.getLocation(&Filename, &Line, &Column);
   D.getLocation(&Filename, &Line, &Column);
-  SourceLocation Loc;
+  SourceLocation DILoc;
   const FileEntry *FE = FileMgr.getFile(Filename);
   const FileEntry *FE = FileMgr.getFile(Filename);
   if (FE && Line > 0) {
   if (FE && Line > 0) {
     // If -gcolumn-info was not used, Column will be 0. This upsets the
     // 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(FE, Line, Column);
+    // 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
+  // function definition. We use the definition's right brace to differentiate
+  // from diagnostics that genuinely relate to the function itself.
+  FullSourceLoc Loc(DILoc, SourceMgr);
+  if (Loc.isInvalid())
+    if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
+      Loc = FD->getASTContext().getFullLoc(FD->getBodyRBrace());
+
   Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName())
   Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName())
                             << D.getMsg().str();
                             << D.getMsg().str();
 
 
@@ -443,13 +450,13 @@ void BackendConsumer::EmitOptimizationRemark(
     // FIXME: We should really be generating !srcloc annotations when
     // FIXME: We should really be generating !srcloc annotations when
     // -Rpass is used. !srcloc annotations need to be emitted in
     // -Rpass is used. !srcloc annotations need to be emitted in
     // approximately the same spots as !dbg nodes.
     // approximately the same spots as !dbg nodes.
-    Diags.Report(diag::note_fe_backend_optimization_remark_missing_loc);
-  else if (Loc.isInvalid())
+    Diags.Report(Loc, diag::note_fe_backend_optimization_remark_missing_loc);
+  else if (DILoc.isInvalid())
     // If we were not able to translate the file:line:col information
     // If we were not able to translate the file:line:col information
     // back to a SourceLocation, at least emit a note stating that
     // back to a SourceLocation, at least emit a note stating that
     // we could not translate this location. This can happen in the
     // we could not translate this location. This can happen in the
     // case of #line directives.
     // case of #line directives.
-    Diags.Report(diag::note_fe_backend_optimization_remark_invalid_loc)
+    Diags.Report(Loc, diag::note_fe_backend_optimization_remark_invalid_loc)
         << Filename << Line << Column;
         << Filename << Line << Column;
 }
 }
 
 

+ 3 - 6
test/Frontend/optimization-remark-line-directive.c

@@ -2,14 +2,11 @@
 // directives. We cannot map #line directives back to
 // directives. We cannot map #line directives back to
 // a SourceLocation.
 // 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
-//
+// RUN: %clang_cc1 %s -Rpass=inline -S -gline-tables-only -dwarf-column-info -emit-llvm-only -verify
+
 int foo(int x, int y) __attribute__((always_inline));
 int foo(int x, int y) __attribute__((always_inline));
 int foo(int x, int y) { return x + y; }
 int foo(int x, int y) { return x + y; }
 
 
+// expected-remark@+2 {{foo inlined into bar}} expected-note@+2 {{could not determine the original source location for /bad/path/to/original.c:1230:25}}
 #line 1230 "/bad/path/to/original.c"
 #line 1230 "/bad/path/to/original.c"
 int bar(int j) { return foo(j, j - 2); }
 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 original source location for /bad/path/to/original.c:1230:0

+ 16 - 14
test/Frontend/optimization-remark.c

@@ -3,10 +3,8 @@
 // always trigger the inliner, so it should be independent of the
 // always trigger the inliner, so it should be independent of the
 // optimization level.
 // optimization level.
 
 
-// RUN: %clang_cc1 %s -Rpass=inline -Rpass-analysis=inline -Rpass-missed=inline -O0 -gline-tables-only -emit-obj -verify -S -o /dev/null 2> %t.err
-
-// RUN: %clang -c %s -Rpass=inline -O0 -S -o /dev/null 2> %t.err
-// RUN: FileCheck < %t.err %s --check-prefix=INLINE-NO-LOC
+// RUN: %clang_cc1 %s -Rpass=inline -Rpass-analysis=inline -Rpass-missed=inline -O0 -gline-tables-only -emit-llvm-only -verify -S
+// RUN: %clang_cc1 %s -DNDEBUG -Rpass=inline -emit-llvm-only -verify -S
 
 
 int foo(int x, int y) __attribute__((always_inline));
 int foo(int x, int y) __attribute__((always_inline));
 int foo(int x, int y) { return x + y; }
 int foo(int x, int y) { return x + y; }
@@ -17,13 +15,17 @@ float foz(int x, int y) { return x * y; }
 // The negative diagnostics are emitted twice because the inliner runs
 // The negative diagnostics are emitted twice because the inliner runs
 // twice.
 // twice.
 //
 //
-// expected-remark@+6 {{foz should never be inlined (cost=never)}}
-// expected-remark@+5 {{foz will not be inlined into bar}}
-// expected-remark@+4 {{foz should never be inlined}}
-// expected-remark@+3 {{foz will not be inlined into bar}}
-// expected-remark@+2 {{foo should always be inlined}}
-// expected-remark@+1 {{foo inlined into bar}}
-int bar(int j) { return foo(j, j - 2) * foz(j - 2, j); }
-
-// INLINE-NO-LOC: {{^remark: foo inlined into bar}}
-// INLINE-NO-LOC: note: use -gline-tables-only -gcolumn-info to track
+int bar(int j) {
+#ifndef NDEBUG
+// expected-remark@+7 {{foz should never be inlined (cost=never)}}
+// expected-remark@+6 {{foz will not be inlined into bar}}
+// expected-remark@+5 {{foz should never be inlined}}
+// expected-remark@+4 {{foz will not be inlined into bar}}
+// expected-remark@+3 {{foo should always be inlined}}
+// expected-remark@+2 {{foo inlined into bar}}
+#endif
+  return foo(j, j - 2) * foz(j - 2, j);
+}
+#ifdef NDEBUG
+// expected-remark@-2 {{foo inlined into bar}} expected-note@-2 {{use -gline-tables-only -gcolumn-info to track source location information for this optimization remark}}
+#endif