[PATCH] [clang] add support for -W{no-,}inline-asm-syntax

Saleem Abdulrasool compnerd at compnerd.org
Thu Feb 20 00:21:30 PST 2014


Hi rengolin, dwmw2,

When the integrated assembler is in use, the compiler is able to validate inline
assembly.  However, in order to maintain compatibility with GCC (and
expectations of user-code), inline assembly should not be validated.  The Linux
kernel, for example, relies on this behaviour to use the compiler as a
preprocessor.

If we are emitting an object file, then the assembly must obviously be parsed,
and as such, assembly processing is only disabled when emitting an assembly
file.  It is possible to still perform the inline assembly validation (by means
of parsing) by passing in the new -Winline-asm-syntax flag.

-Wno-inline-asm-syntax is added for completeness (and can be useful if
-Winline-asm-syntax is passed globally, but a single target needs to have it
disabled as it follows the last specification wins paradigm).

http://llvm-reviews.chandlerc.com/D2840

Files:
  include/clang/Basic/DiagnosticOptions.def
  include/clang/Driver/Options.td
  lib/CodeGen/BackendUtil.cpp
  lib/Driver/Tools.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/inline-asm-diagnostics.c
  test/Driver/inline-asm-syntax.cpp

Index: include/clang/Basic/DiagnosticOptions.def
===================================================================
--- include/clang/Basic/DiagnosticOptions.def
+++ include/clang/Basic/DiagnosticOptions.def
@@ -46,6 +46,7 @@
 
 SEMANTIC_DIAGOPT(IgnoreWarnings, 1, 0)   /// -w
 DIAGOPT(NoRewriteMacros, 1, 0)  /// -Wno-rewrite-macros
+DIAGOPT(InlineASMSyntax, 1, 0)  /// -finline-asm-syntax
 DIAGOPT(Pedantic, 1, 0)         /// -pedantic
 DIAGOPT(PedanticErrors, 1, 0)   /// -pedantic-errors
 DIAGOPT(ShowColumn, 1, 1)       /// Show column number on diagnostics.
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -281,6 +281,10 @@
   MetaVarName<"<arg>">;
 def Wwrite_strings : Flag<["-"], "Wwrite-strings">, Group<W_Group>, Flags<[CC1Option]>;
 def Wno_write_strings : Flag<["-"], "Wno-write-strings">, Group<W_Group>, Flags<[CC1Option]>;
+def Winline_asm_syntax : Flag<["-"], "Winline-asm-syntax">,
+                         Group<CompileOnly_Group>, Flags<[DriverOption]>;
+def Wno_inline_asm_syntax : Flag<["-"], "Wno-inline-asm-syntax">,
+                            Group<CompileOnly_Group>, Flags<[DriverOption]>;
 def W_Joined : Joined<["-"], "W">, Group<W_Group>, Flags<[CC1Option, CoreOption]>,
   MetaVarName<"<warning>">, HelpText<"Enable the specified warning">;
 def Xanalyzer : Separate<["-"], "Xanalyzer">,
@@ -467,6 +471,8 @@
 def : Flag<["-"], "fextended-identifiers">, Group<clang_ignored_f_Group>;
 def : Flag<["-"], "fno-extended-identifiers">, Group<f_Group>, Flags<[Unsupported]>;
 def fhosted : Flag<["-"], "fhosted">, Group<f_Group>;
+def finline_asm_syntax : Flag<["-"], "finline-asm-syntax">, Group<f_clang_Group>,
+                         Flags<[CC1Option]>;
 def ffast_math : Flag<["-"], "ffast-math">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Enable the *frontend*'s 'fast-math' mode. This has no effect on "
            "optimizations, but provides a preprocessor macro __FAST_MATH__ the "
Index: lib/CodeGen/BackendUtil.cpp
===================================================================
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -96,7 +96,8 @@
   /// In this case, we allow this method to fail and not report an error.
   /// When MustCreateTM is used, we print an error if we are unable to load
   /// the requested target.
-  TargetMachine *CreateTargetMachine(bool MustCreateTM);
+  TargetMachine *CreateTargetMachine(bool MustCreateTM,
+                                     bool ValidateInlineAssembly);
 
   /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
   ///
@@ -353,7 +354,8 @@
   PMBuilder.populateModulePassManager(*MPM);
 }
 
-TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
+TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM,
+                                                  bool ValidateInlineAssembly) {
   // Create the TargetMachine for generating code.
   std::string Error;
   std::string Triple = TheModule->getTargetTriple();
@@ -484,6 +486,7 @@
   Options.TrapFuncName = CodeGenOpts.TrapFuncName;
   Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
   Options.EnableSegmentedStacks = CodeGenOpts.EnableSegmentedStacks;
+  Options.ValidateInlineASMSyntax = ValidateInlineAssembly;
 
   TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
                                                      FeaturesStr, Options,
@@ -552,8 +555,11 @@
   bool UsesCodeGen = (Action != Backend_EmitNothing &&
                       Action != Backend_EmitBC &&
                       Action != Backend_EmitLL);
+  bool ValidateInlineAssembly = (Action != Backend_EmitAssembly ||
+                                 Diags.getDiagnosticOptions().InlineASMSyntax);
+
   if (!TM)
-    TM.reset(CreateTargetMachine(UsesCodeGen));
+    TM.reset(CreateTargetMachine(UsesCodeGen, ValidateInlineAssembly));
 
   if (UsesCodeGen && !TM) return;
   CreatePasses();
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -2509,6 +2509,10 @@
       Args.hasArg(options::OPT_dA))
     CmdArgs.push_back("-masm-verbose");
 
+  if (Args.hasFlag(options::OPT_Winline_asm_syntax,
+                   options::OPT_Wno_inline_asm_syntax, false))
+    CmdArgs.push_back("-finline-asm-syntax");
+
   if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
     CmdArgs.push_back("-mdebug-pass");
     CmdArgs.push_back("Structure");
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -545,6 +545,8 @@
     Args.getLastArgValue(OPT_diagnostic_serialized_file);
   Opts.IgnoreWarnings = Args.hasArg(OPT_w);
   Opts.NoRewriteMacros = Args.hasArg(OPT_Wno_rewrite_macros);
+  Opts.InlineASMSyntax = Args.hasArg(OPT_finline_asm_syntax);
+
   Opts.Pedantic = Args.hasArg(OPT_pedantic);
   Opts.PedanticErrors = Args.hasArg(OPT_pedantic_errors);
   Opts.ShowCarets = !Args.hasArg(OPT_fno_caret_diagnostics);
Index: test/CodeGen/inline-asm-diagnostics.c
===================================================================
--- /dev/null
+++ test/CodeGen/inline-asm-diagnostics.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -triple i386 -S -o - %s | FileCheck -check-prefix NO_INLINE_ASM_SYNTAX %s
+// RUN: not %clang_cc1 -triple i386 -finline-asm-syntax -S -o /dev/null %s 2>&1 | FileCheck -check-prefix INLINE_ASM_SYNTAX %s
+// RUN: not %clang_cc1 -emit-obj -triple i386 -o /dev/null %s 2>&1 | FileCheck -check-prefix NO_INLINE_ASM_SYNTAX_OBJ %s
+// RUN: not %clang_cc1 -emit-obj -triple i386 -finline-asm-syntax -o /dev/null %s 2>&1 | FileCheck -check-prefix INLINE_ASM_SYNTAX_OBJ %s
+
+void inline_asm(void) {
+  asm volatile("invalid assembly");
+}
+
+// NO_INLINE_ASM_SYNTAX: #APP
+// NO_INLINE_ASM_SYNTAX: invalid assembly
+// NO_INLINE_ASM_SYNTAX: #NO_APP
+
+// INLINE_ASM_SYNTAX: error: invalid instruction mnemonic 'invalid'
+// INLINE_ASM_SYNTAX:   asm volatile("invalid assembly");
+// INLINE_ASM_SYNTAX:                ^
+
+// NO_INLINE_ASM_SYNTAX_OBJ: error: invalid instruction mnemonic 'invalid'
+// NO_INLINE_ASM_SYNTAX_OBJ:   asm volatile("invalid assembly");
+// NO_INLINE_ASM_SYNTAX_OBJ:                ^
+
+// INLINE_ASM_SYNTAX_OBJ: error: invalid instruction mnemonic 'invalid'
+// INLINE_ASM_SYNTAX_OBJ:   asm volatile("invalid assembly");
+// INLINE_ASM_SYNTAX_OBJ:                ^
+
Index: test/Driver/inline-asm-syntax.cpp
===================================================================
--- /dev/null
+++ test/Driver/inline-asm-syntax.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang -### -Winline-asm-syntax %s 2>&1 \
+//          | FileCheck -check-prefix INLINE_ASM_SYNTAX %s
+// INLINE_ASM_SYNTAX: -finline-asm-syntax
+
+// RUN: %clang -### -Wno-inline-asm-syntax %s 2>&1 \
+//          | FileCheck -check-prefix NO_INLINE_ASM_SYNTAX %s
+// NO_INLINE_ASM_SYNTAX-NOT: -finline-asm-syntax
+
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2840.1.patch
Type: text/x-patch
Size: 7126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140220/dfff084c/attachment.bin>


More information about the llvm-commits mailing list