[PATCH] D102244: [llvm][AsmPrinter] Restore source location to register clobber warning

David Spickett via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 08:30:16 PDT 2021


DavidSpickett created this revision.
Herald added a subscriber: hiraditya.
DavidSpickett requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

Since 5de2d189e6ad466a1f0616195e8c524a4eb3cbc0 <https://reviews.llvm.org/rG5de2d189e6ad466a1f0616195e8c524a4eb3cbc0> this particular warning
hasn't had the location of the source file containing the inline
assembly.

To fix this report the warning and note via MCContext rather than directly
using the source manager.

This message is already tested via IR in llvm. However we won't have
the required location info there so I've added a C file test in clang
to cover it.
(though strictly, this is testing llvm code)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102244

Files:
  clang/test/Misc/inline-asm-clobber-warning.c
  llvm/include/llvm/MC/MCContext.h
  llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
  llvm/lib/MC/MCContext.cpp


Index: llvm/lib/MC/MCContext.cpp
===================================================================
--- llvm/lib/MC/MCContext.cpp
+++ llvm/lib/MC/MCContext.cpp
@@ -964,6 +964,12 @@
   }
 }
 
+void MCContext::reportNote(SMLoc Loc, const Twine &Msg) {
+  reportCommon(Loc, [&](SMDiagnostic &D, const SourceMgr *SMP) {
+    D = SMP->GetMessage(Loc, SourceMgr::DK_Note, Msg);
+  });
+}
+
 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
   reportError(Loc, Msg);
 
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
@@ -542,8 +542,8 @@
         "Reserved registers on the clobber list may not be "
         "preserved across the asm statement, and clobbering them may "
         "lead to undefined behaviour.";
-    SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
-    SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
+    MMI->getContext().reportWarning(Loc, Msg);
+    MMI->getContext().reportNote(Loc, Note);
   }
 
   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
Index: llvm/include/llvm/MC/MCContext.h
===================================================================
--- llvm/include/llvm/MC/MCContext.h
+++ llvm/include/llvm/MC/MCContext.h
@@ -805,6 +805,7 @@
     void diagnose(const SMDiagnostic &SMD);
     void reportError(SMLoc L, const Twine &Msg);
     void reportWarning(SMLoc L, const Twine &Msg);
+    void reportNote(SMLoc L, const Twine &Msg);
     // Unrecoverable error has occurred. Display the best diagnostic we can
     // and bail via exit(1). For now, most MC backend errors are unrecoverable.
     // FIXME: We should really do something about that.
Index: clang/test/Misc/inline-asm-clobber-warning.c
===================================================================
--- /dev/null
+++ clang/test/Misc/inline-asm-clobber-warning.c
@@ -0,0 +1,26 @@
+/// This test checks that the warning includes the location in the C source
+/// file that contains the inline asm. Instead of saying <inline asm> for both.
+/// Although this warning is emitted in llvm it cannot be tested from IR as
+/// it does not have that location information at that stage.
+
+// RUN: %clang -target arm-arm-none-eabi -march=armv7-m -c %s -o /dev/null \
+// RUN:   2>&1 | FileCheck %s
+
+// REQUIRES: arm-registered-target
+
+void bar(void) {
+    __asm__ __volatile__ ( "nop" : : : "sp");
+}
+
+// CHECK:      inline-asm-clobber-warning.c:12:28: warning: inline asm clobber list contains reserved registers: SP [-Winline-asm]
+// CHECK-NEXT:     __asm__ __volatile__ ( "nop" : : : "sp");
+// CHECK-NEXT:                            ^
+// CHECK-NEXT: <inline asm>:1:1: note: instantiated into assembly here
+// CHECK-NEXT:         nop
+// CHECK-NEXT: ^
+// CHECK-NEXT: inline-asm-clobber-warning.c:12:28: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+// CHECK-NEXT:     __asm__ __volatile__ ( "nop" : : : "sp");
+// CHECK-NEXT:                            ^
+// CHECK-NEXT: <inline asm>:1:1: note: instantiated into assembly here
+// CHECK-NEXT:         nop
+// CHECK-NEXT: ^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102244.344412.patch
Type: text/x-patch
Size: 3316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210511/fb786feb/attachment.bin>


More information about the llvm-commits mailing list