[llvm] r372374 - Reapply [llvm-ar] Include a line number when failing to parse an MRI script

Owen Reynolds via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 01:10:14 PDT 2019


Author: gbreynoo
Date: Fri Sep 20 01:10:14 2019
New Revision: 372374

URL: http://llvm.org/viewvc/llvm-project?rev=372374&view=rev
Log:
Reapply [llvm-ar] Include a line number when failing to parse an MRI script

Reapply r372309

Errors that occur when reading an MRI script now include a corresponding
line number.

Differential Revision: https://reviews.llvm.org/D67449

Added:
    llvm/trunk/test/tools/llvm-ar/mri-errors.test
Modified:
    llvm/trunk/test/Object/mri2.test
    llvm/trunk/test/Object/mri3.test
    llvm/trunk/test/Object/mri4.test
    llvm/trunk/test/tools/llvm-ar/mri-addlib.test
    llvm/trunk/tools/llvm-ar/llvm-ar.cpp

Modified: llvm/trunk/test/Object/mri2.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/mri2.test?rev=372374&r1=372373&r2=372374&view=diff
==============================================================================
--- llvm/trunk/test/Object/mri2.test (original)
+++ llvm/trunk/test/Object/mri2.test Fri Sep 20 01:10:14 2019
@@ -4,4 +4,4 @@
 ; RUN: echo end >> %t.mri
 
 ; RUN: not llvm-ar -M  < %t.mri 2>&1 | FileCheck %s
-; CHECK: error: editing multiple archives not supported
+; CHECK: error: script line 2: editing multiple archives not supported

Modified: llvm/trunk/test/Object/mri3.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/mri3.test?rev=372374&r1=372373&r2=372374&view=diff
==============================================================================
--- llvm/trunk/test/Object/mri3.test (original)
+++ llvm/trunk/test/Object/mri3.test Fri Sep 20 01:10:14 2019
@@ -3,4 +3,4 @@
 ; RUN: echo end >> %t.mri
 
 ; RUN: not llvm-ar -M  < %t.mri 2>&1 | FileCheck %s
-; CHECK: error: file already saved
+; CHECK: error: script line 2: file already saved

Modified: llvm/trunk/test/Object/mri4.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/mri4.test?rev=372374&r1=372373&r2=372374&view=diff
==============================================================================
--- llvm/trunk/test/Object/mri4.test (original)
+++ llvm/trunk/test/Object/mri4.test Fri Sep 20 01:10:14 2019
@@ -1,4 +1,4 @@
 ; RUN: echo abc > %t.mri
 
 ; RUN: not llvm-ar -M  < %t.mri 2>&1 | FileCheck %s
-; CHECK: error: unknown command: abc
+; CHECK: error: script line 1: unknown command: abc

Modified: llvm/trunk/test/tools/llvm-ar/mri-addlib.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-ar/mri-addlib.test?rev=372374&r1=372373&r2=372374&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-ar/mri-addlib.test (original)
+++ llvm/trunk/test/tools/llvm-ar/mri-addlib.test Fri Sep 20 01:10:14 2019
@@ -22,10 +22,10 @@
 # RUN: not llvm-ar -M < %t/badlib.mri 2>&1 | FileCheck --check-prefix=PARSE %s
 # RUN: not ls %t/badlib.a
 
-# PARSE: error: could not parse library
+# PARSE: error: script line 2: could not parse library
 
 ## No create command.
-# RUN: echo "ADDLIB %t/f.a"  > %t/nocreate.mri
+# RUN: echo "ADDLIB %t/f.a" > %t/nocreate.mri
 # RUN: echo "SAVE" >> %t/nocreate.mri
 # RUN: not llvm-ar -M < %t/nocreate.mri
 

Added: llvm/trunk/test/tools/llvm-ar/mri-errors.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-ar/mri-errors.test?rev=372374&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-ar/mri-errors.test (added)
+++ llvm/trunk/test/tools/llvm-ar/mri-errors.test Fri Sep 20 01:10:14 2019
@@ -0,0 +1,41 @@
+## Test different MRI comment formats and whitespace.
+
+# RUN: rm -rf %t && mkdir -p %t
+
+# RUN: echo "create %t/mri.ar" > %t/script1.mri
+# RUN: echo "addlib %t/missing.a" >> %t/script1.mri
+
+# RUN: not llvm-ar -M < %t/script1.mri 2>&1 | FileCheck --check-prefix=SCRIPT1 %s
+# SCRIPT1: error: script line 2: could not open library
+
+# RUN: echo "create %t/mri.ar" > %t/script2.mri
+# RUN: echo "addlib %t/bad.a" >> %t/script2.mri
+
+# RUN: echo "bad archive" > %t/bad.a
+
+# RUN: not llvm-ar -M < %t/script2.mri 2>&1 | FileCheck --check-prefix=SCRIPT2 %s
+# SCRIPT2: error: script line 2: could not parse library
+
+# RUN: echo "create %t/mri.ar" > %t/script3.mri
+# RUN: echo "create %t/second.ar" >> %t/script3.mri
+
+# RUN: not llvm-ar -M < %t/script3.mri 2>&1 | FileCheck --check-prefix=SCRIPT3 %s
+# SCRIPT3: error: script line 2: editing multiple archives not supported
+
+# RUN: echo "save" > %t/script4.mri
+# RUN: echo "create %t/mri.ar" >> %t/script4.mri
+
+# RUN: not llvm-ar -M < %t/script4.mri 2>&1 | FileCheck --check-prefix=SCRIPT4 %s
+# SCRIPT4: error: script line 2: file already saved
+
+# RUN: echo "create %t/mri.ar" > %t/script5.mri
+# RUN: echo "bad command" >> %t/script5.mri
+
+# RUN: not llvm-ar -M < %t/script5.mri 2>&1 | FileCheck --check-prefix=SCRIPT5 %s
+# SCRIPT5: error: script line 2: unknown command: bad
+
+# RUN: echo "bad command" | not llvm-ar -M 2>&1 | FileCheck --check-prefix=SCRIPT6 %s
+# SCRIPT6: error: script line 1: unknown command: bad
+
+# RUN: not llvm-ar -M rc %t/mri.ar 2>&1 | FileCheck --check-prefix=SCRIPT7 %s
+# SCRIPT7: error: cannot mix -M and other options

Modified: llvm/trunk/tools/llvm-ar/llvm-ar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/llvm-ar.cpp?rev=372374&r1=372373&r2=372374&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ar/llvm-ar.cpp (original)
+++ llvm/trunk/tools/llvm-ar/llvm-ar.cpp Fri Sep 20 01:10:14 2019
@@ -116,10 +116,19 @@ void printHelpMessage() {
     outs() << ArHelp;
 }
 
+static unsigned MRILineNumber;
+static bool ParsingMRIScript;
+
 // Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
-  WithColor::error(errs(), ToolName) << Error << "\n";
-  printHelpMessage();
+  if (ParsingMRIScript) {
+    WithColor::error(errs(), ToolName)
+        << "script line " << MRILineNumber << ": " << Error << "\n";
+  } else {
+    WithColor::error(errs(), ToolName) << Error << "\n";
+    printHelpMessage();
+  }
+
   exit(1);
 }
 
@@ -958,8 +967,10 @@ static void runMRIScript() {
   const MemoryBuffer &Ref = *Buf.get();
   bool Saved = false;
   std::vector<NewArchiveMember> NewMembers;
+  ParsingMRIScript = true;
 
   for (line_iterator I(Ref, /*SkipBlanks*/ false), E; I != E; ++I) {
+    ++MRILineNumber;
     StringRef Line = *I;
     Line = Line.split(';').first;
     Line = Line.split('*').first;
@@ -1021,7 +1032,9 @@ static void runMRIScript() {
       fail("unknown command: " + CommandStr);
     }
   }
-
+  
+  ParsingMRIScript = false;
+  
   // Nothing to do if not saved.
   if (Saved)
     performOperation(ReplaceOrInsert, &NewMembers);




More information about the llvm-commits mailing list