aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2011-09-02 00:47:55 +0000
committerRichard Trieu <rtrieu@google.com>2011-09-02 00:47:55 +0000
commit09a26ad31d63b3faa9f1fe0041156746da84cdf5 (patch)
treef3edcec7206a29d40b90b7a777b9036158d54ec4
parentf8a1e51c48761ee1d7803c3fa35ac94f42ebb55e (diff)
Refactor CheckAddressOfOperand() by pulling out redundant code and moving hard coding strings from SemaExpr.cpp to DiagnosticSemaKinds.td.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138987 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--lib/Sema/SemaExpr.cpp23
2 files changed, 16 insertions, 10 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index d5f1564e6a..69a4a4d523 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3151,7 +3151,8 @@ def warn_standalone_specifier : Warning<"'%0' ignored on this declaration">;
def err_typecheck_sclass_func : Error<"illegal storage class on function">;
def err_static_block_func : Error<
"function declared in block scope cannot have 'static' storage class">;
-def err_typecheck_address_of : Error<"address of %0 requested">;
+def err_typecheck_address_of : Error<"address of %select{bit-field"
+ "|vector element|property expression|register variable}0 requested">;
def ext_typecheck_addrof_void : Extension<
"ISO C forbids taking the address of an expression of type 'void'">;
def err_unqualified_pointer_member_function : Error<
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 875aefa931..99e8afe82b 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -7201,6 +7201,15 @@ static ValueDecl *getPrimaryDecl(Expr *E) {
}
}
+/// \brief Diagnose invalid operand for address of operations.
+///
+/// \param Type The type of operand which cannot have its address taken.
+/// 0:bit-field 1:vector element 2:property expression 3:register variable
+static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc,
+ Expr *E, unsigned Type) {
+ S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange();
+}
+
/// CheckAddressOfOperand - The operand of & must be either a function
/// designator or an lvalue designating an object. If it is an lvalue, the
/// object cannot be declared with storage class register or be a bit field.
@@ -7287,18 +7296,15 @@ static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp,
}
} else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1
// The operand cannot be a bit-field
- S.Diag(OpLoc, diag::err_typecheck_address_of)
- << "bit-field" << op->getSourceRange();
- return QualType();
+ diagnoseAddressOfInvalidType(S, OpLoc, op, /*bit-field*/ 0);
+ return QualType();
} else if (op->getObjectKind() == OK_VectorComponent) {
// The operand cannot be an element of a vector
- S.Diag(OpLoc, diag::err_typecheck_address_of)
- << "vector element" << op->getSourceRange();
+ diagnoseAddressOfInvalidType(S, OpLoc, op, /*vector element*/ 1);
return QualType();
} else if (op->getObjectKind() == OK_ObjCProperty) {
// cannot take address of a property expression.
- S.Diag(OpLoc, diag::err_typecheck_address_of)
- << "property expression" << op->getSourceRange();
+ diagnoseAddressOfInvalidType(S, OpLoc, op, /*property expression*/ 2);
return QualType();
} else if (dcl) { // C99 6.5.3.2p1
// We have an lvalue with a decl. Make sure the decl is not declared
@@ -7308,8 +7314,7 @@ static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp,
// variable (c++03 7.1.1P3)
if (vd->getStorageClass() == SC_Register &&
!S.getLangOptions().CPlusPlus) {
- S.Diag(OpLoc, diag::err_typecheck_address_of)
- << "register variable" << op->getSourceRange();
+ diagnoseAddressOfInvalidType(S, OpLoc, op, /*register variable*/ 3);
return QualType();
}
} else if (isa<FunctionTemplateDecl>(dcl)) {