[cfe-commits] r39291 - in /cfe/cfe/trunk: AST/SemaExpr.cpp Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:42:34 PDT 2007


Author: sabre
Date: Wed Jul 11 11:42:34 2007
New Revision: 39291

URL: http://llvm.org/viewvc/llvm-project?rev=39291&view=rev
Log:
Diagnose invalid sizeof/alignof operands.

Modified:
    cfe/cfe/trunk/AST/SemaExpr.cpp
    cfe/cfe/trunk/Sema/SemaExpr.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/cfe/trunk/AST/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaExpr.cpp?rev=39291&r1=39290&r2=39291&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/AST/SemaExpr.cpp Wed Jul 11 11:42:34 2007
@@ -336,7 +336,25 @@
                            SourceLocation RParenLoc) {
   // If error parsing type, ignore.
   if (Ty == 0) return true;
-  return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
+  
+  // Verify that this is a valid expression.
+  TypeRef ArgTy = TypeRef::getFromOpaquePtr(Ty);
+  
+  if (isa<FunctionType>(ArgTy) && isSizeof) {
+    // alignof(function) is allowed.
+    Diag(OpLoc, diag::ext_sizeof_function_type);
+    return new IntegerConstant(/*1*/);
+  } else if (ArgTy->isVoidType()) {
+    Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
+  } else if (ArgTy->isIncompleteType()) {
+    std::string TypeName;
+    ArgTy->getAsString(TypeName);
+    Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : 
+         diag::err_alignof_incomplete_type, TypeName);
+    return new IntegerConstant(/*0*/);
+  }
+  
+  return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy);
 }
 
 

Modified: cfe/cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaExpr.cpp?rev=39291&r1=39290&r2=39291&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaExpr.cpp Wed Jul 11 11:42:34 2007
@@ -336,7 +336,25 @@
                            SourceLocation RParenLoc) {
   // If error parsing type, ignore.
   if (Ty == 0) return true;
-  return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
+  
+  // Verify that this is a valid expression.
+  TypeRef ArgTy = TypeRef::getFromOpaquePtr(Ty);
+  
+  if (isa<FunctionType>(ArgTy) && isSizeof) {
+    // alignof(function) is allowed.
+    Diag(OpLoc, diag::ext_sizeof_function_type);
+    return new IntegerConstant(/*1*/);
+  } else if (ArgTy->isVoidType()) {
+    Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
+  } else if (ArgTy->isIncompleteType()) {
+    std::string TypeName;
+    ArgTy->getAsString(TypeName);
+    Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : 
+         diag::err_alignof_incomplete_type, TypeName);
+    return new IntegerConstant(/*0*/);
+  }
+  
+  return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy);
 }
 
 

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=39291&r1=39290&r2=39291&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:42:34 2007
@@ -370,6 +370,7 @@
 DIAG(err_hex_escape_no_digits, ERROR,
      "\\x used with no following hex digits")
 
+// Declarations.
 DIAG(err_typename_requires_specqual, ERROR,
      "type name requires a specifier or qualifier")
 DIAG(err_typename_invalid_storageclass, ERROR,
@@ -434,6 +435,17 @@
 DIAG(ext_implicit_function_decl, EXTENSION,
      "implicit declaration of function '%s' is invalid in C99")
 
+// Expressions.
+DIAG(ext_sizeof_function_type, EXTENSION,
+     "invalid application of 'sizeof' to a function type")
+DIAG(ext_sizeof_void_type, EXTENSION,
+     "invalid application of '%s' to a void type")
+DIAG(err_sizeof_incomplete_type, ERROR,
+     "invalid application of 'sizeof' to an incomplete type '%s'")
+DIAG(err_alignof_incomplete_type, ERROR,
+     "invalid application of '__alignof' to an incomplete type '%s'")
+
+// Statements.
 DIAG(err_continue_not_in_loop, ERROR,
      "'continue' statement not in loop statement")
 DIAG(err_break_not_in_loop_or_switch, ERROR,





More information about the cfe-commits mailing list