[cfe-commits] r145045 - in /cfe/trunk: 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 test/SemaTemplate/instantiation-depth.cpp

Richard Smith richard-llvm at metafoo.co.uk
Mon Nov 21 11:36:32 PST 2011


Author: rsmith
Date: Mon Nov 21 13:36:32 2011
New Revision: 145045

URL: http://llvm.org/viewvc/llvm-project?rev=145045&view=rev
Log:
Add driver arguments -ftemplate-depth=N and -fconstexpr-depth=N, with the same
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.

Added:
    cfe/trunk/test/SemaCXX/constexpr-depth.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Basic/LangOptions.def
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Driver/Options.td
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/test/SemaTemplate/instantiation-depth.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Nov 21 13:36:32 2011
@@ -2384,7 +2384,7 @@
   "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"

Modified: cfe/trunk/include/clang/Basic/LangOptions.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.def?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.def (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.def Mon Nov 21 13:36:32 2011
@@ -145,6 +145,8 @@
 
 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, 

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Mon Nov 21 13:36:32 2011
@@ -584,6 +584,8 @@
   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">;

Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Mon Nov 21 13:36:32 2011
@@ -478,7 +478,9 @@
 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>;

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Nov 21 13:36:32 2011
@@ -257,9 +257,6 @@
     /// 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 @@
 
 
     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 @@
       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 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 @@
                                   const CXXConstructorDecl *Definition,
                                   EvalInfo &Info,
                                   APValue &Result) {
-  if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512)
+  if (Info.atCallLimit())
     return false;
 
   ArgVector ArgValues(Args.size());

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Mon Nov 21 13:36:32 2011
@@ -1619,11 +1619,17 @@
     }
   }
 
-  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");

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Mon Nov 21 13:36:32 2011
@@ -789,6 +789,10 @@
     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 @@
   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);

Added: cfe/trunk/test/SemaCXX/constexpr-depth.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constexpr-depth.cpp?rev=145045&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/constexpr-depth.cpp (added)
+++ cfe/trunk/test/SemaCXX/constexpr-depth.cpp Mon Nov 21 13:36:32 2011
@@ -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);

Modified: cfe/trunk/test/SemaTemplate/instantiation-depth.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiation-depth.cpp?rev=145045&r1=145044&r2=145045&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiation-depth.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiation-depth.cpp Mon Nov 21 13:36:32 2011
@@ -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}}





More information about the cfe-commits mailing list