[PATCH] Adding option -fno-inline-asm to disallow inline asm

Steven Wu stevenwu at apple.com
Wed Jan 7 14:42:59 PST 2015


This patch add a new option to dis-allow all inline asm.
Any GCC style inline asm will be reported as an error.

http://reviews.llvm.org/D6870

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Basic/LangOptions.def
  include/clang/Driver/Options.td
  lib/Driver/Tools.cpp
  lib/Frontend/CompilerInvocation.cpp
  lib/Sema/SemaStmtAsm.cpp
  test/Sema/no-inline-asm.c

Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -6154,6 +6154,8 @@
 
 // inline asm.
 let CategoryName = "Inline Assembly Issue" in {
+  def err_asm_disabled : Error<
+    "inline asm is not allowed with -fno-inline-asm">;
   def err_asm_invalid_lvalue_in_output : Error<"invalid lvalue in asm output">;
   def err_asm_invalid_output_constraint : Error<
     "invalid output constraint '%0' in asm">;
Index: include/clang/Basic/LangOptions.def
===================================================================
--- include/clang/Basic/LangOptions.def
+++ include/clang/Basic/LangOptions.def
@@ -114,6 +114,7 @@
 LANGOPT(Freestanding, 1, 0, "freestanding implementation")
 LANGOPT(NoBuiltin         , 1, 0, "disable builtin functions")
 LANGOPT(NoMathBuiltin     , 1, 0, "disable math builtin functions")
+LANGOPT(NoInlineAsm       , 1, 0, "disable inline assembly")
 
 BENIGN_LANGOPT(ThreadsafeStatics , 1, 1, "thread-safe static initializers")
 LANGOPT(POSIXThreads      , 1, 0, "POSIX thread support")
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -404,6 +404,10 @@
   Flags<[DriverOption, CC1Option]>,
   HelpText<"Disable generation of linker directives for automatic library linking">;
 
+def finline_asm : Flag<["-"], "finline-asm">, Group<f_Group>, Flags<[DriverOption]>;
+def fno_inline_asm : Flag<["-"], "fno-inline-asm">, Group<f_Group>, Flags<[CC1Option]>,
+  HelpText<"Disable inline assembly">;
+
 def fprofile_sample_use_EQ : Joined<["-"], "fprofile-sample-use=">,
     Group<f_Group>, Flags<[DriverOption, CC1Option]>,
     HelpText<"Enable sample-based profile guided optimizations">;
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -4307,6 +4307,11 @@
                    false))
     CmdArgs.push_back("-fasm-blocks");
 
+  // -fno-inline-asm
+  if (Args.hasFlag(options::OPT_fno_inline_asm, options::OPT_finline_asm,
+                   false))
+    CmdArgs.push_back("-fno-inline-asm");
+
   // Enable vectorization per default according to the optimization level
   // selected. For optimization levels that want vectorization we use the alias
   // option to simplify the hasFlag logic.
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -1541,6 +1541,7 @@
       Args.getLastArgValue(OPT_fmodule_implementation_of);
   Opts.NativeHalfType = Opts.NativeHalfType;
   Opts.HalfArgsAndReturns = Args.hasArg(OPT_fallow_half_arguments_and_returns);
+  Opts.NoInlineAsm = Args.hasArg(OPT_fno_inline_asm);
 
   if (!Opts.CurrentModule.empty() && !Opts.ImplementationOfModule.empty() &&
       Opts.CurrentModule != Opts.ImplementationOfModule) {
Index: lib/Sema/SemaStmtAsm.cpp
===================================================================
--- lib/Sema/SemaStmtAsm.cpp
+++ lib/Sema/SemaStmtAsm.cpp
@@ -107,6 +107,10 @@
                                  MultiExprArg constraints, MultiExprArg Exprs,
                                  Expr *asmString, MultiExprArg clobbers,
                                  SourceLocation RParenLoc) {
+  // Check if inline asm is not allowed
+  if (getLangOpts().NoInlineAsm) {
+    return StmtError(Diag(AsmLoc,diag::err_asm_disabled));
+  }
   unsigned NumClobbers = clobbers.size();
   StringLiteral **Constraints =
     reinterpret_cast<StringLiteral**>(constraints.data());
Index: test/Sema/no-inline-asm.c
===================================================================
--- /dev/null
+++ test/Sema/no-inline-asm.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -triple i686-apple-darwin -verify -fsyntax-only -fno-inline-asm
+
+void f (void) {
+  long long foo = 0, bar;
+  asm volatile("INST %0, %1" : "=r"(foo) : "r"(bar)); // expected-error {{inline asm is not allowed with -fno-inline-asm}}
+  return;
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6870.17874.patch
Type: text/x-patch
Size: 4214 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150107/cd443b49/attachment.bin>


More information about the cfe-commits mailing list