From 6602c25c0c78219690f09e727cdb016990e6f3c3 Mon Sep 17 00:00:00 2001
From: Douglas Gregor
Date: Fri, 30 Apr 2010 02:51:06 +0000
Subject: [PATCH] Add Clang version inspection macros. Fixes PR6681.
llvm-svn: 102686
---
clang/docs/LanguageExtensions.html | 39 ++++++++++++++++++++++++-
clang/lib/Frontend/InitPreprocessor.cpp | 16 +++++++++-
clang/test/Preprocessor/init.c | 4 +++
3 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/clang/docs/LanguageExtensions.html b/clang/docs/LanguageExtensions.html
index 9779c3ff441f..838b65f27b81 100644
--- a/clang/docs/LanguageExtensions.html
+++ b/clang/docs/LanguageExtensions.html
@@ -199,7 +199,44 @@ is used in the file argument.
Builtin Macros
-__BASE_FILE__, __INCLUDE_LEVEL__, __TIMESTAMP__, __COUNTER__
+
+ __BASE_FILE__
+ - Defined to a string that contains the name of the main input
+ file passed to Clang.
+
+ __COUNTER__
+ - Defined to an integer value that starts at zero and is
+ incremented each time the
__COUNTER__
macro is
+ expanded.
+
+ __INCLUDE_LEVEL__
+ - Defined to an integral value that is the include depth of the
+ file currently being translated. For the main file, this value is
+ zero.
+
+ __TIMESTAMP__
+ - Defined to the date and time of the last modification of the
+ current source file.
+
+ __clang__
+ - Defined when compiling with Clang
+
+ __clang_major__
+ - Defined to the major version number of Clang (e.g., the 2 in
+ 2.0.1).
+
+ __clang_minor__
+ - Defined to the minor version number of Clang (e.g., the 0 in
+ 2.0.1).
+
+ __clang_patchlevel__
+ - Defined to the patch level of Clang (e.g., the 1 in 2.0.1).
+
+ __clang_version__
+ - Defined to a string that captures the Clang version, including
+ the Subversion tag or revision number, e.g., "1.5 (trunk
+ 102332)".
+
Vectors and Extended Vectors
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index e035afd70779..f1e9819d83ca 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
+#include "clang/Basic/Version.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/MacroBuilder.h"
#include "clang/Basic/TargetInfo.h"
@@ -212,7 +213,20 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
// Compiler version introspection macros.
Builder.defineMacro("__llvm__"); // LLVM Backend
Builder.defineMacro("__clang__"); // Clang Frontend
-
+#define TOSTR2(X) #X
+#define TOSTR(X) TOSTR2(X)
+ Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
+ Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
+#ifdef CLANG_VERSION_PATCHLEVEL
+ Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
+#else
+ Builder.defineMacro("__clang_patchlevel__", "0");
+#endif
+ Builder.defineMacro("__clang_version__",
+ "\"" CLANG_VERSION_STRING " ("
+ + getClangFullRepositoryVersion() + ")\"");
+#undef TOSTR
+#undef TOSTR2
// Currently claim to be compatible with GCC 4.2.1-5621.
Builder.defineMacro("__GNUC_MINOR__", "2");
Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index 4ef460bbc6e4..7909921784b7 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -48,6 +48,10 @@
// COMMON:#define __STDC__ 1
// COMMON:#define __VERSION__
// COMMON:#define __clang__ 1
+// COMMON:#define __clang_major__ 2
+// COMMON:#define __clang_minor__ 0
+// COMMON:#define __clang_patchlevel__ 0
+// COMMON:#define __clang_version__ "2.0 (trunk 102685)"
// COMMON:#define __llvm__ 1
//
//