kito-cheng created this revision.
kito-cheng added reviewers: asb, apazos.
Herald added subscribers: cfe-commits, shiva0217, niosHD, sabuasal, 
jordy.potman.lists, simoncook, johnrusso, rbar.

This patch doing more check and verify the -march= string and will issue and 
error if it's a invalid combination.


Repository:
  rC Clang

https://reviews.llvm.org/D44189

Files:
  lib/Driver/ToolChains/Arch/RISCV.cpp
  test/Driver/riscv-arch.c

Index: test/Driver/riscv-arch.c
===================================================================
--- /dev/null
+++ test/Driver/riscv-arch.c
@@ -0,0 +1,23 @@
+// RUN: %clang -target riscv32-unknown-elf -march=rv32 -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32 %s
+// RV32: error: invalid arch name 'rv32'
+
+// RUN: %clang -target riscv32-unknown-elf -march=rv32m -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32M %s
+// RV32M: error: invalid arch name 'rv32m'
+
+// RUN: %clang -target riscv32-unknown-elf -march=rv32id -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32ID %s
+// RV32ID: error: invalid arch name 'rv32id'
+
+// RUN: %clang -target riscv32-unknown-elf -march=rv32l -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32L %s
+// RV32L: error: invalid arch name 'rv32l'
+
+// RUN: %clang -target riscv64-unknown-elf -march=rv64 -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV64 %s
+// RV64: error: invalid arch name 'rv64'
+
+// RUN: %clang -target riscv64-unknown-elf -march=rv64m -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV64M %s
+// RV64M: error: invalid arch name 'rv64m'
+
+// RUN: %clang -target riscv64-unknown-elf -march=rv64id -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV64ID %s
+// RV64ID: error: invalid arch name 'rv64id'
+
+// RUN: %clang -target riscv64-unknown-elf -march=rv64l -### %s -fsyntax-only 2>&1 | FileCheck -check-prefix=RV64L %s
+// RV64L: error: invalid arch name 'rv64l'
Index: lib/Driver/ToolChains/Arch/RISCV.cpp
===================================================================
--- lib/Driver/ToolChains/Arch/RISCV.cpp
+++ lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -23,33 +23,65 @@
 void riscv::getRISCVTargetFeatures(const Driver &D, const ArgList &Args,
                                    std::vector<StringRef> &Features) {
   if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
-    StringRef MArch = A->getValue();
-    // TODO: handle rv64
-    std::pair<StringRef, StringRef> MArchSplit = StringRef(MArch).split("rv32");
-    if (!MArchSplit.second.size())
+    StringRef March = A->getValue();
+    if (!(March.startswith("rv32") || March.startswith("rv64")) ||
+        (March.size() < 5)) {
+      // ISA string must begin with rv32 or rv64.
+      D.Diag(diag::err_drv_invalid_arch_name) << March;
       return;
+    }
+
+    bool hasF = false, hasD = false;
+    char baseline = March[4];
+
+    switch (baseline) {
+    case 'i':
+      break;
+    case 'g':
+      Features.push_back("+m");
+      Features.push_back("+a");
+      Features.push_back("+f");
+      Features.push_back("+d");
+      hasF = true;
+      hasD = true;
+      break;
+    default:
+      // First letter should be 'i' or 'g'.
+      D.Diag(diag::err_drv_invalid_arch_name) << March;
+      break;
+    }
 
-    for (char c : MArchSplit.second) {
+    // Skip rvxxx
+    StringRef Exts = March.substr(5);
+
+    for (char c : Exts) {
       switch (c) {
-      case 'i':
-        break;
       case 'm':
         Features.push_back("+m");
         break;
       case 'a':
         Features.push_back("+a");
         break;
       case 'f':
         Features.push_back("+f");
+        hasF = true;
         break;
       case 'd':
         Features.push_back("+d");
+        hasD = true;
         break;
       case 'c':
         Features.push_back("+c");
         break;
+      default:
+        D.Diag(diag::err_drv_invalid_arch_name) << March;
+        break;
       }
     }
+
+    // Dependency check
+    if (hasD && !hasF)
+      D.Diag(diag::err_drv_invalid_arch_name) << March;
   }
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to