[llvm] d81ce04 - [Driver] Report error for unsupported -mlarge-endian/-mlittle-endian

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue May 30 12:45:27 PDT 2023


Author: Fangrui Song
Date: 2023-05-30T12:45:21-07:00
New Revision: d81ce04587c006b6731198956c522c93d0df1050

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

LOG: [Driver] Report error for unsupported -mlarge-endian/-mlittle-endian

Added: 
    clang/test/Driver/endian.c

Modified: 
    clang/include/clang/Driver/Options.td
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/ppc-endian.c
    llvm/include/llvm/Option/ArgList.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index f3bfc26f271cc..f6240f86447cb 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3452,9 +3452,9 @@ def l : JoinedOrSeparate<["-"], "l">, Flags<[LinkerInput, RenderJoined]>,
         Group<Link_Group>;
 def lazy__framework : Separate<["-"], "lazy_framework">, Flags<[LinkerInput]>;
 def lazy__library : Separate<["-"], "lazy_library">, Flags<[LinkerInput]>;
-def mlittle_endian : Flag<["-"], "mlittle-endian">, Flags<[NoXarchOption]>;
+def mlittle_endian : Flag<["-"], "mlittle-endian">, Flags<[NoXarchOption,TargetSpecific]>;
 def EL : Flag<["-"], "EL">, Alias<mlittle_endian>;
-def mbig_endian : Flag<["-"], "mbig-endian">, Flags<[NoXarchOption]>;
+def mbig_endian : Flag<["-"], "mbig-endian">, Flags<[NoXarchOption,TargetSpecific]>;
 def EB : Flag<["-"], "EB">, Alias<mbig_endian>;
 def m16 : Flag<["-"], "m16">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>;
 def m32 : Flag<["-"], "m32">, Group<m_Group>, Flags<[NoXarchOption, CoreOption]>;

diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ade59f45384fd..fee14ba6faabe 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -559,16 +559,14 @@ static llvm::Triple computeTargetTriple(const Driver &D,
 
   // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
   // '-mbig-endian'/'-EB'.
-  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
-                               options::OPT_mbig_endian)) {
-    if (A->getOption().matches(options::OPT_mlittle_endian)) {
-      llvm::Triple LE = Target.getLittleEndianArchVariant();
-      if (LE.getArch() != llvm::Triple::UnknownArch)
-        Target = std::move(LE);
-    } else {
-      llvm::Triple BE = Target.getBigEndianArchVariant();
-      if (BE.getArch() != llvm::Triple::UnknownArch)
-        Target = std::move(BE);
+  if (Arg *A = Args.getLastArgNoClaim(options::OPT_mlittle_endian,
+                                      options::OPT_mbig_endian)) {
+    llvm::Triple T = A->getOption().matches(options::OPT_mlittle_endian)
+                         ? Target.getLittleEndianArchVariant()
+                         : Target.getBigEndianArchVariant();
+    if (T.getArch() != llvm::Triple::UnknownArch) {
+      Target = std::move(T);
+      Args.claimAllArgs(options::OPT_mlittle_endian, options::OPT_mbig_endian);
     }
   }
 

diff  --git a/clang/test/Driver/endian.c b/clang/test/Driver/endian.c
new file mode 100644
index 0000000000000..7fddbc93e9cd6
--- /dev/null
+++ b/clang/test/Driver/endian.c
@@ -0,0 +1,5 @@
+// RUN: %clang -### -c --target=x86_64 -mbig-endian -mlittle-endian %s 2>&1 | FileCheck /dev/null --implicit-check-not=error:
+// RUN: %clang -### -c --target=x86_64 -mlittle-endian -mbig-endian %s 2>&1 | FileCheck %s --implicit-check-not=error:
+
+// CHECK: error: unsupported option '-mlittle-endian' for target 'x86_64'
+// CHECK: error: unsupported option '-mbig-endian' for target 'x86_64'

diff  --git a/clang/test/Driver/ppc-endian.c b/clang/test/Driver/ppc-endian.c
index 00c1c25bb358f..5854fa63fdc83 100644
--- a/clang/test/Driver/ppc-endian.c
+++ b/clang/test/Driver/ppc-endian.c
@@ -1,5 +1,5 @@
 // RUN: %clang -target powerpc-unknown -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-BE32 %s
-// RUN: %clang -target powerpc-unknown -mbig-endian -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-BE32 %s
+// RUN: %clang --target=powerpc-unknown -mbig-endian -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-BE32 %s --implicit-check-not=error:
 // RUN: %clang -target powerpcle-unknown -mbig-endian -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-BE32 %s
 // CHECK-BE32: "-cc1"{{.*}} "-triple" "powerpc-{{.*}}"
 

diff  --git a/llvm/include/llvm/Option/ArgList.h b/llvm/include/llvm/Option/ArgList.h
index 6a07e1c657dca..310c8900af9ef 100644
--- a/llvm/include/llvm/Option/ArgList.h
+++ b/llvm/include/llvm/Option/ArgList.h
@@ -354,6 +354,12 @@ class ArgList {
   /// option id.
   void ClaimAllArgs(OptSpecifier Id0) const;
 
+  template <typename... OptSpecifiers>
+  void claimAllArgs(OptSpecifiers... Ids) const {
+    for (Arg *A : filtered(Ids...))
+      A->claim();
+  }
+
   /// ClaimAllArgs - Claim all arguments.
   ///
   void ClaimAllArgs() const;


        


More information about the llvm-commits mailing list