Attempt to fix incorrect diagnoses of diag::err_template_arg_too_large

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Attempt to fix incorrect diagnoses of diag::err_template_arg_too_large

Maurice
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)


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Attempt to fix incorrect diagnoses of diag::err_template_arg_too_large

Eli Friedman
On Wed, Dec 23, 2009 at 4:57 AM, Maurice Gittens
<[hidden email]> wrote:

> 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.

Interesting bug report, but the patch doesn't look right; I'll take a look.

-Eli
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Loading...