bruno updated this revision to Diff 165612.
bruno added a comment.

Addressed Adrian's review. Added support to consider the environment and well 
the combination platform-environment. @aprantl also handled multiple variants 
of simulator combinations.


https://reviews.llvm.org/D51910

Files:
  docs/Modules.rst
  lib/Basic/Module.cpp
  lib/Lex/ModuleMap.cpp
  test/Modules/target-platform-features.m

Index: test/Modules/target-platform-features.m
===================================================================
--- /dev/null
+++ test/Modules/target-platform-features.m
@@ -0,0 +1,83 @@
+// Clear and create directories
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: mkdir %t/cache
+// RUN: mkdir %t/InputsA
+
+// RUN: echo "module RequiresMacOS {"       >> %t/InputsA/module.map
+// RUN: echo "  requires macos"             >> %t/InputsA/module.map
+// RUN: echo "}"                            >> %t/InputsA/module.map
+// RUN: echo "module RequiresNotiOS {"      >> %t/InputsA/module.map
+// RUN: echo "  requires !ios"              >> %t/InputsA/module.map
+// RUN: echo "}"                            >> %t/InputsA/module.map
+// RUN: echo "module RequiresMain {"        >> %t/InputsA/module.map
+// RUN: echo "  module SubRequiresNotiOS {" >> %t/InputsA/module.map
+// RUN: echo "    requires !ios"            >> %t/InputsA/module.map
+// RUN: echo "  }"                          >> %t/InputsA/module.map
+// RUN: echo "}"                            >> %t/InputsA/module.map
+
+// RUN: %clang_cc1 -triple=x86_64-apple-macosx10.6 -DENABLE_DARWIN -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsA -verify %s 
+// expected-no-diagnostics
+
+// RUN: not %clang_cc1 -triple=arm64-apple-ios -DENABLE_DARWIN -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsA %s  2> %t/notios
+// RUN: FileCheck %s -check-prefix=CHECK-IOS < %t/notios
+#ifdef ENABLE_DARWIN
+// CHECK-IOS: module 'RequiresMacOS' requires feature 'macos'
+@import RequiresMacOS;
+// CHECK-IOS: module 'RequiresNotiOS' is incompatible with feature 'ios'
+@import RequiresNotiOS;
+// We should never get errors for submodules that don't match
+// CHECK-IOS-NOT: module 'RequiresMain'
+@import RequiresMain;
+#endif
+
+// RUN: mkdir %t/InputsB
+// RUN: echo "module RequiresiOSSimA {"     >> %t/InputsB/module.map
+// RUN: echo "  requires iossimulator"      >> %t/InputsB/module.map
+// RUN: echo "}"                            >> %t/InputsB/module.map
+// RUN: echo "module RequiresiOSSimB {"     >> %t/InputsB/module.map
+// RUN: echo "  requires ios-simulator"     >> %t/InputsB/module.map
+// RUN: echo "}"                            >> %t/InputsB/module.map
+// RUN: %clang_cc1 -triple=x86_64-apple-iossimulator -DENABLE_IOSSIM -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsB %s  -verify
+// RUN: %clang_cc1 -triple=x86_64-apple-ios-simulator -DENABLE_IOSSIM -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsB %s  -verify
+
+#ifdef ENABLE_IOSSIM
+@import RequiresiOSSimA;
+@import RequiresiOSSimB;
+#endif
+
+// RUN: mkdir %t/InputsC
+// RUN: echo "module RequiresLinuxEABIA {"  >> %t/InputsC/module.map
+// RUN: echo "  requires linux-gnueabi"     >> %t/InputsC/module.map
+// RUN: echo "}"                            >> %t/InputsC/module.map
+// RUN: echo "module RequiresLinuxEABIB {"  >> %t/InputsC/module.map
+// RUN: echo "  requires gnueabi"           >> %t/InputsC/module.map
+// RUN: echo "}"                            >> %t/InputsC/module.map
+// RUN: echo "module RequiresLinuxEABIC {"  >> %t/InputsC/module.map
+// RUN: echo "  requires linux"             >> %t/InputsC/module.map
+// RUN: echo "}"                            >> %t/InputsC/module.map
+// RUN: %clang_cc1 -triple=armv8r-none-linux-gnueabi -DENABLE_LINUXEABI -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsC %s -verify
+
+#ifdef ENABLE_LINUXEABI
+@import RequiresLinuxEABIA;
+@import RequiresLinuxEABIB;
+@import RequiresLinuxEABIC;
+#endif
+
+// RUN: mkdir %t/InputsD
+// RUN: echo "module RequiresWinMSVCA {"  >> %t/InputsD/module.map
+// RUN: echo "  requires windows"         >> %t/InputsD/module.map
+// RUN: echo "}"                          >> %t/InputsD/module.map
+// RUN: echo "module RequiresWinMSVCB {"  >> %t/InputsD/module.map
+// RUN: echo "  requires windows-msvc"    >> %t/InputsD/module.map
+// RUN: echo "}"                          >> %t/InputsD/module.map
+// RUN: echo "module RequiresWinMSVCC {"  >> %t/InputsD/module.map
+// RUN: echo "  requires msvc"            >> %t/InputsD/module.map
+// RUN: echo "}"                          >> %t/InputsD/module.map
+// RUN: %clang_cc1 -triple=thumbv7-unknown-windows-msvc -DENABLE_WINMSVC -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -x objective-c -I%t/InputsD %s  -verify
+
+#ifdef ENABLE_WINMSVC
+@import RequiresWinMSVCA;
+@import RequiresWinMSVCB;
+@import RequiresWinMSVCC;
+#endif
Index: lib/Lex/ModuleMap.cpp
===================================================================
--- lib/Lex/ModuleMap.cpp
+++ lib/Lex/ModuleMap.cpp
@@ -1285,6 +1285,7 @@
       FrameworkKeyword,
       LinkKeyword,
       ModuleKeyword,
+      Minus,
       Period,
       PrivateKeyword,
       UmbrellaKeyword,
@@ -1484,6 +1485,10 @@
     Tok.Kind = MMToken::LSquare;
     break;
 
+  case tok::minus:
+    Tok.Kind = MMToken::Minus;
+    break;
+
   case tok::period:
     Tok.Kind = MMToken::Period;
     break;
@@ -2144,6 +2149,7 @@
 ///
 ///   feature:
 ///     '!'[opt] identifier
+///     '!'[opt] identifier '-' identifier
 void ModuleMapParser::parseRequiresDecl() {
   assert(Tok.is(MMToken::RequiresKeyword));
 
@@ -2164,10 +2170,23 @@
       return;
     }
 
-    // Consume the feature name.
+    // Consume the feature or platform name.
     std::string Feature = Tok.getString();
     consumeToken();
 
+    // Consume enviornment after platform name.
+    if (Tok.is(MMToken::Minus)) {
+      consumeToken();
+      if (!Tok.is(MMToken::Identifier)) {
+        Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
+        HadError = true;
+        return;
+      }
+      Feature.append("-");
+      Feature.append(Tok.getString());
+      consumeToken();
+    }
+
     bool IsRequiresExcludedHack = false;
     bool ShouldAddRequirement =
         shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
@@ -2896,6 +2915,7 @@
     case MMToken::LBrace:
     case MMToken::LinkKeyword:
     case MMToken::LSquare:
+    case MMToken::Minus:
     case MMToken::Period:
     case MMToken::PrivateKeyword:
     case MMToken::RBrace:
Index: lib/Basic/Module.cpp
===================================================================
--- lib/Basic/Module.cpp
+++ lib/Basic/Module.cpp
@@ -71,6 +71,41 @@
   }
 }
 
+static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
+  StringRef Platform = Target.getPlatformName();
+  StringRef Env = Target.getTriple().getEnvironmentName();
+
+  // Attempt to match platform/environment in the following order:
+  // 1. Platform
+  // 2, Environment
+  // 3. Platform-Environment
+  if (Platform == Feature || Target.getTriple().getOSName() == Feature ||
+      Env == Feature)
+    return true;
+
+  auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
+    auto Pos = LHS.find("-");
+    if (Pos == StringRef::npos)
+      return false;
+    SmallString<128> NewLHS = LHS.slice(0, Pos);
+    NewLHS += LHS.slice(Pos+1, LHS.size());
+    return NewLHS == RHS;
+  };
+
+  SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
+  // Darwin has different but equivalent variants for simulators, example:
+  //   1. x86_64-apple-ios-simulator
+  //   2. x86_64-apple-iossimulator
+  // where both are valid examples of the same platform+environment but in the
+  // variant (2), the simulator is hardcoded as part of the platform name. Both
+  // forms above should match "iossimulator" and "ios-simulator" features.
+  if (Target.getTriple().isOSDarwin() && PlatformEnv.endswith("simulator"))
+    return PlatformEnv == Feature || CmpPlatformEnv(Feature, PlatformEnv) ||
+           CmpPlatformEnv(PlatformEnv, Feature);
+
+  return PlatformEnv == Feature;
+}
+
 /// Determine whether a translation unit built using the current
 /// language options has the given feature.
 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
@@ -93,7 +128,8 @@
                         .Case("opencl", LangOpts.OpenCL)
                         .Case("tls", Target.isTLSSupported())
                         .Case("zvector", LangOpts.ZVector)
-                        .Default(Target.hasFeature(Feature));
+                        .Default(Target.hasFeature(Feature) ||
+                                 isPlatformEnvironment(Target, Feature));
   if (!HasFeature)
     HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
                            LangOpts.ModuleFeatures.end(),
Index: docs/Modules.rst
===================================================================
--- docs/Modules.rst
+++ docs/Modules.rst
@@ -410,8 +410,9 @@
 
   *feature*:
     ``!``:sub:`opt` *identifier*
+    ``!``:sub:`opt` *identifier* '-' *identifier*
 
-The requirements clause allows specific modules or submodules to specify that they are only accessible with certain language dialects or on certain platforms. The feature list is a set of identifiers, defined below. If any of the features is not available in a given translation unit, that translation unit shall not import the module. When building a module for use by a compilation, submodules requiring unavailable features are ignored. The optional ``!`` indicates that a feature is incompatible with the module.
+The requirements clause allows specific modules or submodules to specify that they are only accessible with certain language dialects, platforms, environments and target specific features. The feature list is a set of identifiers, defined below. If any of the features is not available in a given translation unit, that translation unit shall not import the module. When building a module for use by a compilation, submodules requiring unavailable features are ignored. The optional ``!`` indicates that a feature is incompatible with the module.
 
 The following features are defined:
 
@@ -466,6 +467,14 @@
 *target feature*
   A specific target feature (e.g., ``sse4``, ``avx``, ``neon``) is available.
 
+*platform/os*
+  A os/platform variant (e.g. ``freebsd``, ``win32``, ``windows``, ``linux``, ``ios``, ``macos``, ``iossimulator``) is available.
+
+*environment*
+  A environment variant (e.g. ``gnu``, ``gnueabi``, ``android``, ``msvc``) is available.
+
+*platform-environment*
+  A platform-environment variant (e.g. ``linux-gnueabi``, ``windows-msvc``) is available.
 
 **Example:** The ``std`` module can be extended to also include C++ and C++11 headers using a *requires-declaration*:
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D51910: [... Bruno Cardoso Lopes via Phabricator via cfe-commits

Reply via email to