]> granicus.if.org Git - clang/commitdiff
Add a header containing the Clang version; make the driver use this
authorDouglas Gregor <dgregor@apple.com>
Mon, 27 Apr 2009 22:23:34 +0000 (22:23 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 27 Apr 2009 22:23:34 +0000 (22:23 +0000)
Clang version value rather than hard-coding "1.0".

Add PCH and Clang version information into the PCH file. Reject PCH
files with the wrong version information.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70264 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Basic/Version.h [new file with mode: 0644]
include/clang/Frontend/PCHBitCodes.h
include/clang/Frontend/PCHWriter.h
lib/Driver/Driver.cpp
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp

index 55d1dfacb88a63f3afcb5276618d9a8ef3b2f5df..64a1777924287a8bff6e7c66c98b8ee64661ba46 100644 (file)
@@ -115,4 +115,8 @@ def warn_pch_preprocessor : Warning<
     "current translation unit">;
 def note_predef_in_pch : Note<
     "preprocessor definitions in PCH file">;
+def warn_pch_version_too_old : Warning<
+    "PCH file uses an older PCH format that is no longer supported">;
+def warn_pch_version_too_new : Warning<
+    "PCH file uses a newer PCH format that cannot be read">;
 }
diff --git a/include/clang/Basic/Version.h b/include/clang/Basic/Version.h
new file mode 100644 (file)
index 0000000..f0e1aa7
--- /dev/null
@@ -0,0 +1,35 @@
+//===- Version.h - Clang Version Number -------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This header defines version macros for Clang.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_BASIC_VERSION_H
+#define LLVM_CLANG_BASIC_VERSION_H
+
+/// \brief Clang major version
+#define CLANG_VERSION_MAJOR 1
+
+/// \brief Clang minor version
+#define CLANG_VERSION_MINOR 0
+
+/// \brief Helper macro for CLANG_VERSION_STRING.
+#define CLANG_MAKE_VERSION_STRING2(X) #X
+
+/// \brief Helper macro for CLANG_VERSION_STRING.
+#define CLANG_MAKE_VERSION_STRING(X,Y) CLANG_MAKE_VERSION_STRING2(X.Y)
+
+/// \brief A string that describes the Clang version number, e.g.,
+/// "1.0".
+#define CLANG_VERSION_STRING \
+  CLANG_MAKE_VERSION_STRING(CLANG_VERSION_MAJOR,CLANG_VERSION_MINOR)
+
+
+#endif // LLVM_CLANG_BASIC_VERSION_H
index 7f5e7c500f5776b4a833a338dbe3478a760f6248..a4da70438626c1fe390d96745ee0bfb1ed01f999 100644 (file)
 
 namespace clang {
   namespace pch {
+    /// \brief PCH major version number supported by this version of
+    /// Clang.
+    ///
+    /// Whenever the PCH format changes in a way that makes it
+    /// incompatible with previous versions (such that a reader
+    /// designed for the previous version could not support reading
+    /// the new version), this number should be increased.
+    const unsigned VERSION_MAJOR = 1;
+
+    /// \brief PCH minor version number supported by this version of
+    /// Clang.
+    ///
+    /// Whenever the PCH format changes in a way that is still
+    /// compatible with previous versions (such that a reader designed
+    /// for the previous version could still support reading the new
+    /// version by ignoring new kinds of subblocks), this number
+    /// should be increased.
+    const unsigned VERSION_MINOR = 0;
+
     /// \brief An ID number that refers to a declaration in a PCH file.
     ///
     /// The ID numbers of types are consecutive (in order of
@@ -108,9 +127,9 @@ namespace clang {
       /// actually important to check.
       LANGUAGE_OPTIONS = 3,
 
-      /// \brief Record code for the target triple used to build the
-      /// PCH file.
-      TARGET_TRIPLE = 4,
+      /// \brief PCH metadata, including the PCH file version number
+      /// and the target triple used to build the PCH file.
+      METADATA = 4,
 
       /// \brief Record code for the table of offsets of each
       /// identifier ID.
index b16058c1540e6ca809ca2389e3a79d94fae0699d..3c2d5ff1aff00a2d80806410fc0d5141f7d9068f 100644 (file)
@@ -160,7 +160,7 @@ private:
   unsigned NumVisibleDeclContexts;
 
   void WriteBlockInfoBlock();
-  void WriteTargetTriple(const TargetInfo &Target);
+  void WriteMetadata(const TargetInfo &Target);
   void WriteLanguageOptions(const LangOptions &LangOpts);
   void WriteStatCache(MemorizeStatCalls &StatCalls);
   void WriteSourceManagerBlock(SourceManager &SourceMgr, 
index d88f1b359857a130b5d7bb6dbcb92c283c1934b6..d6a046e9c3248b9f00e31e5dd9d872ff4210f5f5 100644 (file)
@@ -22,6 +22,8 @@
 #include "clang/Driver/ToolChain.h"
 #include "clang/Driver/Types.h"
 
+#include "clang/Basic/Version.h"
+
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/raw_ostream.h"
@@ -353,8 +355,8 @@ void Driver::PrintVersion(const Compilation &C) const {
   // FIXME: The following handlers should use a callback mechanism, we
   // don't know what the client would like to do.
 
-  // FIXME: Do not hardcode clang version.
-  llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
+  llvm::errs() << "clang version " CLANG_VERSION_STRING " (" 
+               << vers << " " << revision << ")" << "\n";
 
   const ToolChain &TC = C.getDefaultToolChain();
   llvm::errs() << "Target: " << TC.getTripleString() << '\n';
@@ -366,8 +368,7 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
   // in practice.
 
   if (C.getArgs().hasArg(options::OPT_dumpversion)) {
-    // FIXME: Do not hardcode clang version.
-    llvm::outs() << "1.0\n";
+    llvm::outs() << CLANG_VERSION_STRING "\n";
     return false;
   }
 
index 24e4e2b02fb3d34cc35bd71d0b93182c46fb092b..c1cb87bc160d78866db01f5a5db55d84d1e8f3ff 100644 (file)
@@ -928,7 +928,13 @@ PCHReader::ReadPCHBlock() {
         return IgnorePCH;
       break;
 
-    case pch::TARGET_TRIPLE: {
+    case pch::METADATA: {
+      if (Record[0] != pch::VERSION_MAJOR) {
+        Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
+                                           : diag::warn_pch_version_too_new);
+        return IgnorePCH;
+      }
+
       std::string TargetTriple(BlobStart, BlobLen);
       if (TargetTriple != PP.getTargetInfo().getTargetTriple()) {
         Diag(diag::warn_pch_target_triple)
index 817893448a70b19b2030e6000c5819b1e1567224..6b58e250920d0bd32bb492f839820bb7cbc34a81 100644 (file)
@@ -27,6 +27,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/SourceManagerInternals.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/Version.h"
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
@@ -341,7 +342,7 @@ void PCHWriter::WriteBlockInfoBlock() {
   RECORD(TYPE_OFFSET);
   RECORD(DECL_OFFSET);
   RECORD(LANGUAGE_OPTIONS);
-  RECORD(TARGET_TRIPLE);
+  RECORD(METADATA);
   RECORD(IDENTIFIER_OFFSET);
   RECORD(IDENTIFIER_TABLE);
   RECORD(EXTERNAL_DEFINITIONS);
@@ -440,18 +441,26 @@ void PCHWriter::WriteBlockInfoBlock() {
 }
 
 
-/// \brief Write the target triple (e.g., i686-apple-darwin9).
-void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
+/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
+void PCHWriter::WriteMetadata(const TargetInfo &Target) {
   using namespace llvm;
   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
-  Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
-  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
-  unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
+  Abbrev->Add(BitCodeAbbrevOp(pch::METADATA));
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
+  unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
 
   RecordData Record;
-  Record.push_back(pch::TARGET_TRIPLE);
+  Record.push_back(pch::METADATA);
+  Record.push_back(pch::VERSION_MAJOR);
+  Record.push_back(pch::VERSION_MINOR);
+  Record.push_back(CLANG_VERSION_MAJOR);
+  Record.push_back(CLANG_VERSION_MINOR);
   const char *Triple = Target.getTargetTriple();
-  Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
+  Stream.EmitRecordWithBlob(AbbrevCode, Record, Triple, strlen(Triple));
 }
 
 /// \brief Write the LangOptions structure.
@@ -1672,7 +1681,7 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls) {
   // Write the remaining PCH contents.
   RecordData Record;
   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
-  WriteTargetTriple(Context.Target);
+  WriteMetadata(Context.Target);
   WriteLanguageOptions(Context.getLangOptions());
   if (StatCalls)
     WriteStatCache(*StatCalls);