[PATCH] D22313: [MC] Add command-line option to choose the max nest level in asm macros

t83wCSLq via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 25 15:47:31 PDT 2016


t83wCSLq updated this revision to Diff 65432.
t83wCSLq added a comment.

Fixing space typo.


https://reviews.llvm.org/D22313

Files:
  lib/MC/MCParser/AsmParser.cpp
  test/MC/AsmParser/macro-max-depth.s

Index: test/MC/AsmParser/macro-max-depth.s
===================================================================
--- /dev/null
+++ test/MC/AsmParser/macro-max-depth.s
@@ -0,0 +1,21 @@
+// RUN: llvm-mc -triple x86_64-unknown-unknown -asm-macro-max-nesting-depth=42 %s | FileCheck %s -check-prefix=CHECK_PASS
+// RUN: not llvm-mc -triple x86_64-unknown-unknown %s 2> %t
+// RUN: FileCheck -check-prefix=CHECK_FAIL < %t %s
+
+.macro rec head, tail:vararg
+	.ifnb	\tail
+	rec	\tail
+	.else
+	.long	42
+	.endif
+.endm
+
+.macro amplify macro, args:vararg
+	\macro	\args \args \args \args
+.endm
+
+amplify rec 0 0 0 0 0 0 0 0 0 0
+
+// CHECK_PASS: .long	42
+// CHECK_FAIL: error: macros cannot be nested more than {{[0-9]+}} levels deep. Use -asm-macro-max-nesting-depth to increase this limit.
+
Index: lib/MC/MCParser/AsmParser.cpp
===================================================================
--- lib/MC/MCParser/AsmParser.cpp
+++ lib/MC/MCParser/AsmParser.cpp
@@ -34,6 +34,7 @@
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/MC/MCValue.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -41,12 +42,17 @@
 #include "llvm/Support/raw_ostream.h"
 #include <cctype>
 #include <deque>
+#include <sstream>
 #include <string>
 #include <vector>
 using namespace llvm;
 
 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
 
+static cl::opt<unsigned> AsmMacroMaxNestingDepth(
+     "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
+     cl::desc("The maximum nesting depth allowed for assembly macros."));
+
 namespace {
 /// \brief Helper types for tracking macro definitions.
 typedef std::vector<AsmToken> MCAsmMacroArgument;
@@ -2359,10 +2365,17 @@
 void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
 
 bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
-  // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
-  // this, although we should protect against infinite loops.
-  if (ActiveMacros.size() == 20)
-    return TokError("macros cannot be nested more than 20 levels deep");
+  // Arbitrarily limit macro nesting depth (default matches 'as'). We can
+  // eliminate this, although we should protect against infinite loops.
+  unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
+  if (ActiveMacros.size() == MaxNestingDepth) {
+    std::ostringstream MaxNestingDepthError;
+    MaxNestingDepthError << "macros cannot be nested more than "
+                         << MaxNestingDepth << " levels deep."
+                         << " Use -asm-macro-max-nesting-depth to increase "
+                            "this limit.";
+    return TokError(MaxNestingDepthError.str());
+  }
 
   MCAsmMacroArguments A;
   if (parseMacroArguments(M, A))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22313.65432.patch
Type: text/x-patch
Size: 2868 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160725/c9fb250e/attachment.bin>


More information about the llvm-commits mailing list