[cfe-dev] A patch for integer promotions.

Enea Zaffanella zaffanella at cs.unipr.it
Sun Aug 16 02:45:33 PDT 2009


Hello.

The attached program promotion_bug.c shows a bug in the current code 
generating integer promotions: independently from the sizes of integer 
types, all small integers are promoted to type `int'.

The bug can only be observed when selecting a 16 bit target 
architecture, e.g., pic16 (but the bug is caused by target-independent 
clang code). The following is the ast dump + comments showing evidence 
for the bug:

# clang-cc -ast-dump -triple=pic16-unknown-unknown promotion_bug.c

int main() (CompoundStmt 0x2b896c0 <promotion_bug.c:1:16, line:8:1>
   (DeclStmt 0x2b8b6c0 <line:2:3, col:31>
     0x2b8b670 "int size_of_int[2]"        <=== sizeof(int) = 2
   (DeclStmt 0x2b86f60 <line:3:3, col:37>
     0x2b86f10 "short size_of_short[2]"    <=== sizeof(short) = 2
   (DeclStmt 0x2b8a860 <line:5:3, col:25>
     0x2b86f90 "unsigned short us =
       (ImplicitCastExpr 0x2b86fe0 <col:23> 'unsigned short'
         (IntegerLiteral 0x2b7b0c0 <col:23> 'int' 10))"
   (DeclStmt 0x2b8a920 <line:6:3, col:13>
     0x2b8a890 "int i =
       (IntegerLiteral 0x2b8a8e0 <col:11> 'int' 20)"
   (BinaryOperator 0x2b94800 <line:7:3, col:12> 'int' '='
     (DeclRefExpr 0x2b8a950 <col:3> 'int' Var='i' 0x2b8a890)
     (BinaryOperator 0x2b8aa90 <col:7, col:12> 'int' '+'
       (ImplicitCastExpr 0x2b8aa10 <col:7> 'int'   <== buggy promotion
         (DeclRefExpr 0x2b8a990 <col:7> 'unsigned short' Var='us' 
0x2b86f90))
       (ImplicitCastExpr 0x2b8aa50 <col:12> 'int'  <== buggy promotion
         (DeclRefExpr 0x2b8a9d0 <col:12> 'unsigned short' Var='us' 
0x2b86f90)))))


The attached patch adds new method
   QualType ASTContext::getPromotedIntegerType(QualType Promotable);
and uses it when generating implicit casts encoding promotions.

After applying the patch, we obtain correct promotions.

Cheers,
Enea Zaffanella.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: promotion_bug.c
Type: text/x-csrc
Size: 145 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090816/15a6eeec/attachment.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: promotion_bug.patch
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090816/15a6eeec/attachment.ksh>


More information about the cfe-dev mailing list