[llvm] r330529 - [bcanalyzer] Recognize more stream types

Brian Gesiak via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 21 16:52:04 PDT 2018


Author: modocache
Date: Sat Apr 21 16:52:04 2018
New Revision: 330529

URL: http://llvm.org/viewvc/llvm-project?rev=330529&view=rev
Log:
[bcanalyzer] Recognize more stream types

Summary:
`llvm-bcanalyzer` prints out the stream type of the file it is
analyzing. If the file begins with the LLVM IR magic number, it reports
a stream type of "LLVM IR". However, any other bitstream format is
reported as "unknown".

Add some checks for two other common bitstream formats: Clang AST
files, which begin with 'CPCH', and Clang serialized diagnostics, which
begin with 'DIAG'.

Test Plan: `check-llvm`

Reviewers: pcc, aprantl, mehdi_amini, davide, george.karpenkov, JDevlieghere

Reviewed By: JDevlieghere

Subscribers: JDevlieghere, bruno, davide, llvm-commits

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

Added:
    llvm/trunk/test/Bitcode/stream-types.c
    llvm/trunk/test/Bitcode/stream-types.c.ast
    llvm/trunk/test/Bitcode/stream-types.c.ast.incomplete
    llvm/trunk/test/Bitcode/stream-types.c.dia
    llvm/trunk/test/Bitcode/stream-types.c.dia.incomplete
Modified:
    llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Added: llvm/trunk/test/Bitcode/stream-types.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/stream-types.c?rev=330529&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/stream-types.c (added)
+++ llvm/trunk/test/Bitcode/stream-types.c Sat Apr 21 16:52:04 2018
@@ -0,0 +1,12 @@
+// Tests that llvm-bcanalyzer recognizes the correct "stream type" for various
+// common bitstream formats.
+
+// RUN: llvm-bcanalyzer -dump %s.ast | FileCheck %s -check-prefix=CHECK-AST
+// CHECK-AST: Stream type: Clang Serialized AST
+
+// RUN: llvm-bcanalyzer -dump %s.dia | FileCheck %s -check-prefix=CHECK-DIAG
+// CHECK-DIAG: Stream type: Clang Serialized Diagnostics
+
+// RUN: not llvm-bcanalyzer -dump %s.ast.incomplete 2>&1 | FileCheck %s -check-prefix=CHECK-INCOMPLETE
+// RUN: not llvm-bcanalyzer -dump %s.dia.incomplete 2>&1 | FileCheck %s -check-prefix=CHECK-INCOMPLETE
+// CHECK-INCOMPLETE: Bitcode stream should be a multiple of 4 bytes in length

Added: llvm/trunk/test/Bitcode/stream-types.c.ast
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/stream-types.c.ast?rev=330529&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/stream-types.c.ast (added)
+++ llvm/trunk/test/Bitcode/stream-types.c.ast Sat Apr 21 16:52:04 2018
@@ -0,0 +1 @@
+CPCH
\ No newline at end of file

Added: llvm/trunk/test/Bitcode/stream-types.c.ast.incomplete
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/stream-types.c.ast.incomplete?rev=330529&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/stream-types.c.ast.incomplete (added)
+++ llvm/trunk/test/Bitcode/stream-types.c.ast.incomplete Sat Apr 21 16:52:04 2018
@@ -0,0 +1 @@
+CP
\ No newline at end of file

Added: llvm/trunk/test/Bitcode/stream-types.c.dia
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/stream-types.c.dia?rev=330529&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/stream-types.c.dia (added)
+++ llvm/trunk/test/Bitcode/stream-types.c.dia Sat Apr 21 16:52:04 2018
@@ -0,0 +1 @@
+DIAG
\ No newline at end of file

Added: llvm/trunk/test/Bitcode/stream-types.c.dia.incomplete
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/stream-types.c.dia.incomplete?rev=330529&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/stream-types.c.dia.incomplete (added)
+++ llvm/trunk/test/Bitcode/stream-types.c.dia.incomplete Sat Apr 21 16:52:04 2018
@@ -0,0 +1 @@
+DIA
\ No newline at end of file

Modified: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=330529&r1=330528&r2=330529&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Sat Apr 21 16:52:04 2018
@@ -75,7 +75,9 @@ namespace {
 /// CurStreamTypeType - A type for CurStreamType
 enum CurStreamTypeType {
   UnknownBitstream,
-  LLVMIRBitstream
+  LLVMIRBitstream,
+  ClangSerializedASTBitstream,
+  ClangSerializedDiagnosticsBitstream,
 };
 
 }
@@ -746,6 +748,35 @@ static void PrintSize(uint64_t Bits) {
                    (double)Bits/8, (unsigned long)(Bits/32));
 }
 
+static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) {
+  char Signature[6];
+  Signature[0] = Stream.Read(8);
+  Signature[1] = Stream.Read(8);
+
+  // Autodetect the file contents, if it is one we know.
+  if (Signature[0] == 'C' && Signature[1] == 'P') {
+    Signature[2] = Stream.Read(8);
+    Signature[3] = Stream.Read(8);
+    if (Signature[2] == 'C' && Signature[3] == 'H')
+      return ClangSerializedASTBitstream;
+  } else if (Signature[0] == 'D' && Signature[1] == 'I') {
+    Signature[2] = Stream.Read(8);
+    Signature[3] = Stream.Read(8);
+    if (Signature[2] == 'A' && Signature[3] == 'G')
+      return ClangSerializedDiagnosticsBitstream;
+  } else {
+    Signature[2] = Stream.Read(4);
+    Signature[3] = Stream.Read(4);
+    Signature[4] = Stream.Read(4);
+    Signature[5] = Stream.Read(4);
+    if (Signature[0] == 'B' && Signature[1] == 'C' &&
+        Signature[2] == 0x0 && Signature[3] == 0xC &&
+        Signature[4] == 0xE && Signature[5] == 0xD)
+      return LLVMIRBitstream;
+  }
+  return UnknownBitstream;
+}
+
 static bool openBitcodeFile(StringRef Path,
                             std::unique_ptr<MemoryBuffer> &MemBuf,
                             BitstreamCursor &Stream,
@@ -789,22 +820,7 @@ static bool openBitcodeFile(StringRef Pa
   }
 
   Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
-
-  // Read the stream signature.
-  char Signature[6];
-  Signature[0] = Stream.Read(8);
-  Signature[1] = Stream.Read(8);
-  Signature[2] = Stream.Read(4);
-  Signature[3] = Stream.Read(4);
-  Signature[4] = Stream.Read(4);
-  Signature[5] = Stream.Read(4);
-
-  // Autodetect the file contents, if it is one we know.
-  CurStreamType = UnknownBitstream;
-  if (Signature[0] == 'B' && Signature[1] == 'C' &&
-      Signature[2] == 0x0 && Signature[3] == 0xC &&
-      Signature[4] == 0xE && Signature[5] == 0xD)
-    CurStreamType = LLVMIRBitstream;
+  CurStreamType = ReadSignature(Stream);
 
   return false;
 }
@@ -873,8 +889,18 @@ static int AnalyzeBitcode() {
   outs() << "\n";
   outs() << "        Stream type: ";
   switch (CurStreamType) {
-  case UnknownBitstream: outs() << "unknown\n"; break;
-  case LLVMIRBitstream:  outs() << "LLVM IR\n"; break;
+  case UnknownBitstream:
+    outs() << "unknown\n";
+    break;
+  case LLVMIRBitstream:
+    outs() << "LLVM IR\n";
+    break;
+  case ClangSerializedASTBitstream:
+    outs() << "Clang Serialized AST\n";
+    break;
+  case ClangSerializedDiagnosticsBitstream:
+    outs() << "Clang Serialized Diagnostics\n";
+    break;
   }
   outs() << "  # Toplevel Blocks: " << NumTopBlocks << "\n";
   outs() << "\n";




More information about the llvm-commits mailing list