]> granicus.if.org Git - clang/commitdiff
Add driver arguments -ftemplate-depth=N and -fconstexpr-depth=N, with the same
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 21 Nov 2011 19:36:32 +0000 (19:36 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 21 Nov 2011 19:36:32 +0000 (19:36 +0000)
semantics and defaults as the corresponding g++ arguments. The historical g++
argument -ftemplate-depth-N is kept for compatibility, but modern g++ versions
no longer document that option.

Add -cc1 argument -fconstexpr-depth N to implement the corresponding
functionality.

The -ftemplate-depth=N part of this fixes PR9890.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145045 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/LangOptions.def
include/clang/Driver/CC1Options.td
include/clang/Driver/Options.td
lib/AST/ExprConstant.cpp
lib/Driver/Tools.cpp
lib/Frontend/CompilerInvocation.cpp
test/SemaCXX/constexpr-depth.cpp [new file with mode: 0644]
test/SemaTemplate/instantiation-depth.cpp

index 6c5e79a9019fbcec12cc7cf8eb24ef2d9fba5b2b..c8b138f2c76ec7416878484556f3d6b9a3c9d83f 100644 (file)
@@ -2384,7 +2384,7 @@ def err_template_recursion_depth_exceeded : Error<
   "recursive template instantiation exceeded maximum depth of %0">,
   DefaultFatal, NoSFINAE;
 def note_template_recursion_depth : Note<
-  "use -ftemplate-depth-N to increase recursive template instantiation depth">;
+  "use -ftemplate-depth=N to increase recursive template instantiation depth">;
 
 def err_template_instantiate_within_definition : Error<
   "%select{implicit|explicit}0 instantiation of template %1 within its"
index 5cf40ec578f7796c3e6acd8f56a5b71c3c6e6253..1936be1fb67a310087277d2ba550810457d5493e 100644 (file)
@@ -145,6 +145,8 @@ ENUM_LANGOPT(SignedOverflowBehavior, SignedOverflowBehaviorTy, 2, SOB_Undefined,
 
 BENIGN_LANGOPT(InstantiationDepth, 32, 1024, 
                "maximum template instantiation depth")
+BENIGN_LANGOPT(ConstexprCallDepth, 32, 512,
+               "maximum constexpr call depth")
 BENIGN_LANGOPT(NumLargeByValueCopy, 32, 0, 
         "if non-zero, warn about parameter or return Warn if parameter/return value is larger in bytes than this setting. 0 is no check.")
 VALUE_LANGOPT(MSCVersion, 32, 0, 
index c8f4a1d77e3a6e26667ea23e0dd0522f06f4ca78..01e4e4b687509c7fd4a61010a6f5780057eb9d5d 100644 (file)
@@ -584,6 +584,8 @@ def fvisibility_inlines_hidden : Flag<"-fvisibility-inlines-hidden">,
   HelpText<"Give inline C++ member functions default visibility by default">;
 def ftemplate_depth : Separate<"-ftemplate-depth">,
   HelpText<"Maximum depth of recursive template instantiation">;
+def fconstexpr_depth : Separate<"-fconstexpr-depth">,
+  HelpText<"Maximum depth of recursive constexpr function calls">;
 def Wlarge_by_value_copy : Separate<"-Wlarge-by-value-copy">,
   HelpText<"Warn if a function definition returns or accepts an object larger "
            "in bytes that a given value">;
index 1cda33d758e94279db7f7abf11f6b7e3303a2e70..470ca5716a9f7f8e35d5fcf48c09cdbf7ef182cd 100644 (file)
@@ -478,7 +478,9 @@ def fstrict_aliasing : Flag<"-fstrict-aliasing">, Group<f_Group>;
 def fstrict_overflow : Flag<"-fstrict-overflow">, Group<f_Group>;
 def fsyntax_only : Flag<"-fsyntax-only">, Flags<[DriverOption]>;
 def ftabstop_EQ : Joined<"-ftabstop=">, Group<f_Group>;
+def ftemplate_depth_EQ : Joined<"-ftemplate-depth=">, Group<f_Group>;
 def ftemplate_depth_ : Joined<"-ftemplate-depth-">, Group<f_Group>;
+def fconstexpr_depth_EQ : Joined<"-fconstexpr-depth=">, Group<f_Group>;
 def ftemplate_backtrace_limit_EQ : Joined<"-ftemplate-backtrace-limit=">,
                                    Group<f_Group>;
 def ftest_coverage : Flag<"-ftest-coverage">, Group<f_Group>;
index d602be4581ef4e4451d8b3ea0a4d17931298c5ce..81fe7e3a4e1b10350968f9225bcf3698b3f40dad 100644 (file)
@@ -257,9 +257,6 @@ namespace {
     /// CurrentCall - The top of the constexpr call stack.
     CallStackFrame *CurrentCall;
 
-    /// NumCalls - The number of calls we've evaluated so far.
-    unsigned NumCalls;
-
     /// CallStackDepth - The number of calls in the call stack right now.
     unsigned CallStackDepth;
 
@@ -282,7 +279,7 @@ namespace {
 
 
     EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
-      : Ctx(C), EvalStatus(S), CurrentCall(0), NumCalls(0), CallStackDepth(0),
+      : Ctx(C), EvalStatus(S), CurrentCall(0), CallStackDepth(0),
         BottomFrame(*this, 0, 0), EvaluatingDecl(0), EvaluatingDeclValue(0) {}
 
     const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const {
@@ -296,7 +293,11 @@ namespace {
       EvaluatingDeclValue = &Value;
     }
 
-    const LangOptions &getLangOpts() { return Ctx.getLangOptions(); }
+    const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); }
+
+    bool atCallLimit() const {
+      return CallStackDepth > getLangOpts().ConstexprCallDepth;
+    }
   };
 
   CallStackFrame::CallStackFrame(EvalInfo &Info, const LValue *This,
@@ -1278,8 +1279,7 @@ static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
 static bool HandleFunctionCall(const LValue *This, ArrayRef<const Expr*> Args,
                                const Stmt *Body, EvalInfo &Info,
                                CCValue &Result) {
-  // FIXME: Implement a proper call limit, along with a command-line flag.
-  if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512)
+  if (Info.atCallLimit())
     return false;
 
   ArgVector ArgValues(Args.size());
@@ -1296,7 +1296,7 @@ static bool HandleConstructorCall(const LValue &This,
                                   const CXXConstructorDecl *Definition,
                                   EvalInfo &Info,
                                   APValue &Result) {
-  if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512)
+  if (Info.atCallLimit())
     return false;
 
   ArgVector ArgValues(Args.size());
index 4229987f5236ca901de14c2fbdb35c83bba9e374..918b4551c5798da7144b5ec913986cd3bc9ee87c 100644 (file)
@@ -1619,11 +1619,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     }
   }
 
-  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
+  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
+                               options::OPT_ftemplate_depth_EQ)) {
     CmdArgs.push_back("-ftemplate-depth");
     CmdArgs.push_back(A->getValue(Args));
   }
 
+  if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
+    CmdArgs.push_back("-fconstexpr-depth");
+    CmdArgs.push_back(A->getValue(Args));
+  }
+
   if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
                                options::OPT_Wlarge_by_value_copy_def)) {
     CmdArgs.push_back("-Wlarge-by-value-copy");
index 738facf0695c69ceaf6a72e4fac4612947a4aec7..34080908c0d1dfafda03490ce1e78d1e3347eb8c 100644 (file)
@@ -789,6 +789,10 @@ static void LangOptsToArgs(const LangOptions &Opts,
     Res.push_back("-ftemplate-depth");
     Res.push_back(llvm::utostr(Opts.InstantiationDepth));
   }
+  if (Opts.ConstexprCallDepth != DefaultLangOpts.ConstexprCallDepth) {
+    Res.push_back("-fconstexpr-depth");
+    Res.push_back(llvm::utostr(Opts.ConstexprCallDepth));
+  }
   if (!Opts.ObjCConstantStringClass.empty()) {
     Res.push_back("-fconstant-string-class");
     Res.push_back(Opts.ObjCConstantStringClass);
@@ -1777,7 +1781,9 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
   Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
   Opts.MathErrno = Args.hasArg(OPT_fmath_errno);
   Opts.InstantiationDepth = Args.getLastArgIntValue(OPT_ftemplate_depth, 1024,
-                                               Diags);
+                                                    Diags);
+  Opts.ConstexprCallDepth = Args.getLastArgIntValue(OPT_fconstexpr_depth, 512,
+                                                    Diags);
   Opts.DelayedTemplateParsing = Args.hasArg(OPT_fdelayed_template_parsing);
   Opts.NumLargeByValueCopy = Args.getLastArgIntValue(OPT_Wlarge_by_value_copy,
                                                     0, Diags);
diff --git a/test/SemaCXX/constexpr-depth.cpp b/test/SemaCXX/constexpr-depth.cpp
new file mode 100644 (file)
index 0000000..b8ae668
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -DMAX=128 -fconstexpr-depth 128
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -DMAX=1 -fconstexpr-depth 1
+// RUN: %clang -std=c++11 -fsyntax-only -Xclang -verify %s -DMAX=10 -fconstexpr-depth=10
+
+constexpr int depth(int n) { return n > 1 ? depth(n-1) : 0; }
+
+constexpr int kBad = depth(MAX + 1); // expected-error {{must be initialized by a constant expression}}
+constexpr int kGood = depth(MAX);
index a2b9d2eb1513c02358b51e361540b07dfa518e46..8e1b80368d18d18721051ac2ef159c83792ef7e7 100644 (file)
@@ -1,10 +1,12 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -ftemplate-depth 5 -ftemplate-backtrace-limit 4 %s
+// RUN: %clang -fsyntax-only -Xclang -verify -ftemplate-depth-5 -ftemplate-backtrace-limit=4 %s
+// RUN: %clang -fsyntax-only -Xclang -verify -ftemplate-depth=5 -ftemplate-backtrace-limit=4 %s
 
 template<typename T> struct X : X<T*> { }; \
 // expected-error{{recursive template instantiation exceeded maximum depth of 5}} \
 // expected-note 3 {{instantiation of template class}} \
 // expected-note {{skipping 2 contexts in backtrace}} \
-// expected-note {{use -ftemplate-depth-N to increase recursive template instantiation depth}}
+// expected-note {{use -ftemplate-depth=N to increase recursive template instantiation depth}}
 
 void test() { 
   (void)sizeof(X<int>); // expected-note {{instantiation of template class}}