[cfe-dev] Attempt to fix incorrect diagnoses of diag::err_template_arg_too_large

Maurice Gittens mainmanmauricio at gmail.com
Wed Dec 23 04:57:33 PST 2009


Hi,

without the following patch, clang incorrectly claims
that, for example, -128 does not fit in a signed char
when used as a template argument.

I found this when trying to use clang++ to parse a boost header
file.

I hope that you find it useful.

Kind regards,
Maurice


diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index e6bd77d..2e260e0 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -2489,12 +2489,24 @@ bool
Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
       // Check that we don't overflow the template parameter type.
       unsigned AllowedBits = Context.getTypeSize(IntegerType);
       if (Value.getActiveBits() > AllowedBits) {
-        Diag(Arg->getSourceRange().getBegin(),
-             diag::err_template_arg_too_large)
+        bool ConstantFits = false;
+
+        if (IntegerType->isSignedIntegerType()) {
+          llvm::APSInt TmpValue = Value;
+          TmpValue.extOrTrunc(AllowedBits);
+
+          if (TmpValue.isMinSignedValue())
+            ConstantFits = true;
+        }
+
+        if (!ConstantFits) {
+          Diag(Arg->getSourceRange().getBegin(),
+          diag::err_template_arg_too_large)
           << Value.toString(10) << Param->getType()
           << Arg->getSourceRange();
-        Diag(Param->getLocation(), diag::note_template_param_here);
-        return true;
+          Diag(Param->getLocation(), diag::note_template_param_here);
+          return true;
+        }
       }

       if (Value.getBitWidth() != AllowedBits)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20091223/bc485ec8/attachment.html>


More information about the cfe-dev mailing list