Restore cfg file printing in Checker as an option

Test: run checker with test that is expected to fail
Change-Id: I14ca5e6bed38e2677cdf4b2eb4b79a6594af6f13
diff --git a/test/run-test b/test/run-test
index 86d30d5..7221832 100755
--- a/test/run-test
+++ b/test/run-test
@@ -928,6 +928,7 @@
       fi
 
       run_args+=(-Xcompiler-option "--dump-cfg=$cfg_output_dir/$cfg_output" -Xcompiler-option -j1)
+      checker_args="$checker_args --print-cfg"
     fi
   fi
 fi
diff --git a/tools/checker/checker.py b/tools/checker/checker.py
index 65b01a7..ad4cdd2 100755
--- a/tools/checker/checker.py
+++ b/tools/checker/checker.py
@@ -39,19 +39,23 @@
                       help="Run tests for the specified target architecture.")
   parser.add_argument("--debuggable", action="store_true",
                       help="Run tests for debuggable code.")
+  parser.add_argument("--print-cfg", action="store_true", default="True", dest="print_cfg",
+                      help="Print the whole cfg file in case of test failure (default)")
+  parser.add_argument("--no-print-cfg", action="store_false", default="True", dest="print_cfg",
+                      help="Don't print the whole cfg file in case of test failure")
   parser.add_argument("-q", "--quiet", action="store_true",
                       help="print only errors")
   return parser.parse_args()
 
 
 def ListPasses(outputFilename):
-  c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
+  c1File = ParseC1visualizerStream(outputFilename, open(outputFilename, "r"))
   for compiler_pass in c1File.passes:
     Logger.log(compiler_pass.name)
 
 
 def DumpPass(outputFilename, passName):
-  c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
+  c1File = ParseC1visualizerStream(outputFilename, open(outputFilename, "r"))
   compiler_pass = c1File.findPass(passName)
   if compiler_pass:
     maxLineNo = compiler_pass.startLineNo + len(compiler_pass.body)
@@ -85,14 +89,14 @@
     Logger.fail("Source path \"" + path + "\" not found")
 
 
-def RunTests(checkPrefix, checkPath, outputFilename, targetArch, debuggableMode):
-  c1File = ParseC1visualizerStream(os.path.basename(outputFilename), open(outputFilename, "r"))
+def RunTests(checkPrefix, checkPath, outputFilename, targetArch, debuggableMode, printCfg):
+  c1File = ParseC1visualizerStream(outputFilename, open(outputFilename, "r"))
   for checkFilename in FindCheckerFiles(checkPath):
     checkerFile = ParseCheckerStream(os.path.basename(checkFilename),
                                      checkPrefix,
                                      open(checkFilename, "r"),
                                      targetArch)
-    MatchFiles(checkerFile, c1File, targetArch, debuggableMode)
+    MatchFiles(checkerFile, c1File, targetArch, debuggableMode, printCfg)
 
 
 if __name__ == "__main__":
@@ -106,4 +110,5 @@
   elif args.dump_pass:
     DumpPass(args.tested_file, args.dump_pass)
   else:
-    RunTests(args.check_prefix, args.source_path, args.tested_file, args.arch, args.debuggable)
+    RunTests(args.check_prefix, args.source_path, args.tested_file, args.arch, args.debuggable,
+             args.print_cfg)
diff --git a/tools/checker/file_format/c1visualizer/parser.py b/tools/checker/file_format/c1visualizer/parser.py
index a31bc56..b25d2b2 100644
--- a/tools/checker/file_format/c1visualizer/parser.py
+++ b/tools/checker/file_format/c1visualizer/parser.py
@@ -96,12 +96,16 @@
     else:
       Logger.fail("C1visualizer line not inside a group", fileName, lineNo)
 
+
 def ParseC1visualizerStream(fileName, stream):
   c1File = C1visualizerFile(fileName)
   state = C1ParserState()
-  fnProcessLine = lambda line, lineNo: __parseC1Line(c1File, line, lineNo, state, fileName)
-  fnLineOutsideChunk = lambda line, lineNo: \
-      Logger.fail("C1visualizer line not inside a group", fileName, lineNo)
+
+  def fnProcessLine(line, lineNo):
+    return __parseC1Line(c1File, line, lineNo, state, c1File.baseFileName)
+
+  def fnLineOutsideChunk(line, lineNo):
+    Logger.fail("C1visualizer line not inside a group", c1File.baseFileName, lineNo)
   for passName, passLines, startLineNo, testArch in \
       SplitStream(stream, fnProcessLine, fnLineOutsideChunk):
     C1visualizerPass(c1File, passName, passLines, startLineNo + 1)
diff --git a/tools/checker/file_format/c1visualizer/struct.py b/tools/checker/file_format/c1visualizer/struct.py
index 21036da..5925da9 100644
--- a/tools/checker/file_format/c1visualizer/struct.py
+++ b/tools/checker/file_format/c1visualizer/struct.py
@@ -12,6 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import os
+
 from common.immutables import ImmutableDict
 from common.logger     import Logger
 from common.mixins     import PrintableMixin
@@ -19,7 +21,8 @@
 class C1visualizerFile(PrintableMixin):
 
   def __init__(self, fileName):
-    self.fileName = fileName
+    self.baseFileName = os.path.basename(fileName)
+    self.fullFileName = fileName
     self.passes = []
     self.instructionSetFeatures = ImmutableDict()
 
@@ -58,7 +61,7 @@
 
   @property
   def fileName(self):
-    return self.parent.fileName
+    return self.parent.baseFileName
 
   def __eq__(self, other):
     return isinstance(other, self.__class__) \
diff --git a/tools/checker/match/file.py b/tools/checker/match/file.py
index 4a0923c..1f68666 100644
--- a/tools/checker/match/file.py
+++ b/tools/checker/match/file.py
@@ -12,12 +12,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from collections                      import namedtuple
-from common.immutables                import ImmutableDict
-from common.logger                    import Logger
-from file_format.c1visualizer.struct  import C1visualizerFile, C1visualizerPass
-from file_format.checker.struct       import CheckerFile, TestCase, TestStatement
-from match.line                       import MatchLines, EvaluateLine
+from collections import namedtuple
+
+from common.immutables import ImmutableDict
+from common.logger import Logger
+from file_format.checker.struct import TestStatement
+from match.line import MatchLines, EvaluateLine
 
 MatchScope = namedtuple("MatchScope", ["start", "end"])
 MatchInfo = namedtuple("MatchInfo", ["scope", "variables"])
@@ -324,7 +324,7 @@
   for statement in testStatements:
     state.handle(statement)
 
-def MatchFiles(checkerFile, c1File, targetArch, debuggableMode):
+def MatchFiles(checkerFile, c1File, targetArch, debuggableMode, printCfg):
   for testCase in checkerFile.testCases:
     if testCase.testArch not in [None, targetArch]:
       continue
@@ -336,10 +336,10 @@
     # match a check group against the first output group of the same name.
     c1Pass = c1File.findPass(testCase.name)
     if c1Pass is None:
-      with file(c1File.fileName) as cfgFile:
+      with file(c1File.fullFileName) as cfgFile:
         Logger.log(''.join(cfgFile), Logger.Level.Error)
       Logger.fail("Test case not found in the CFG file",
-                  testCase.fileName, testCase.startLineNo, testCase.name)
+                  testCase.fullFileName, testCase.startLineNo, testCase.name)
 
     Logger.startTest(testCase.name)
     try:
@@ -352,4 +352,7 @@
       else:
         msg = "Statement could not be matched starting from line {}"
       msg = msg.format(lineNo)
+      if printCfg:
+        with file(c1File.fullFileName) as cfgFile:
+          Logger.log(''.join(cfgFile), Logger.Level.Error)
       Logger.testFailed(msg, e.statement, e.variables)