[llvm-branch-commits] [flang] 197d9a5 - [flang][driver] Add standard macro predefinitions for compiler version

Andrzej Warzynski via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 19 05:27:52 PST 2021


Author: Faris Rehman
Date: 2021-01-19T13:22:59Z
New Revision: 197d9a55f105391f34a0657e6c1d5ef3166dad7d

URL: https://github.com/llvm/llvm-project/commit/197d9a55f105391f34a0657e6c1d5ef3166dad7d
DIFF: https://github.com/llvm/llvm-project/commit/197d9a55f105391f34a0657e6c1d5ef3166dad7d.diff

LOG: [flang][driver] Add standard macro predefinitions for compiler version

Add the following standard predefinitions that f18 supports:
  * `__flang__`,
  * `__flang_major__`,
  * `__flang_minor__`,
  * `__flang_patchlevel__`

Summary of changes:
- Populate Fortran::parser::Options#predefinitions with the default
  supported predefinitions

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

Added: 
    flang/test/Flang-Driver/predefined-macros-compiler-version.f90

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index aeb4ac3e274a..cd0faf215a5a 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -8,6 +8,7 @@
 
 #include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/PreprocessorOptions.h"
+#include "flang/Version.inc"
 #include "clang/Basic/AllDiagnostics.h"
 #include "clang/Basic/DiagnosticDriver.h"
 #include "clang/Basic/DiagnosticOptions.h"
@@ -258,6 +259,18 @@ void CompilerInvocation::SetDefaultFortranOpts() {
   std::vector<std::string> searchDirectories{"."s};
   fortranOptions.searchDirectories = searchDirectories;
   fortranOptions.isFixedForm = false;
+
+  // Populate the macro list with version numbers and other predefinitions.
+  // TODO: When expanding this list of standard predefinitions, consider
+  // creating a dedicated API for this. Also at some point we will need to
+  // 
diff erentiate between 
diff erent targets.
+  fortranOptions.predefinitions.emplace_back("__flang__", "1");
+  fortranOptions.predefinitions.emplace_back(
+      "__flang_major__", FLANG_VERSION_MAJOR_STRING);
+  fortranOptions.predefinitions.emplace_back(
+      "__flang_minor__", FLANG_VERSION_MINOR_STRING);
+  fortranOptions.predefinitions.emplace_back(
+      "__flang_patchlevel__", FLANG_VERSION_PATCHLEVEL_STRING);
 }
 
 void CompilerInvocation::setFortranOpts() {

diff  --git a/flang/test/Flang-Driver/predefined-macros-compiler-version.f90 b/flang/test/Flang-Driver/predefined-macros-compiler-version.f90
new file mode 100644
index 000000000000..bf59f305e884
--- /dev/null
+++ b/flang/test/Flang-Driver/predefined-macros-compiler-version.f90
@@ -0,0 +1,26 @@
+! Check that the driver correctly defines macros with the compiler version
+
+! REQUIRES: new-flang-driver
+
+!--------------------------
+! FLANG DRIVER (flang-new)
+!--------------------------
+! RUN: %flang-new -E %s  2>&1 | FileCheck %s --ignore-case
+
+!-----------------------------------------
+! FRONTEND FLANG DRIVER (flang-new -fc1)
+!-----------------------------------------
+! RUN: %flang-new -fc1 -E %s  2>&1 | FileCheck %s --ignore-case
+
+!-----------------
+! EXPECTED OUTPUT
+!-----------------
+! CHECK: flang = 1
+! CHECK: flang_major = {{[1-9][0-9]*$}}
+! CHECK: flang_minor = {{[0-9]+$}}
+! CHECK: flang_patchlevel = {{[0-9]+$}}
+
+integer, parameter :: flang = __flang__
+integer, parameter :: flang_major = __flang_major__
+integer, parameter :: flang_minor = __flang_minor__
+integer, parameter :: flang_patchlevel = __flang_patchlevel__


        


More information about the llvm-branch-commits mailing list