[cfe-commits] r85129 - in /cfe/trunk: include/clang/Basic/PartialDiagnostic.h lib/Sema/SemaExpr.cpp
Sebastian Redl
sebastian.redl at getdesigned.at
Mon Oct 26 10:01:33 PDT 2009
Author: cornedbee
Date: Mon Oct 26 12:01:32 2009
New Revision: 85129
URL: http://llvm.org/viewvc/llvm-project?rev=85129&view=rev
Log:
Add fixit hint to bitwise precedence warning.
Modified:
cfe/trunk/include/clang/Basic/PartialDiagnostic.h
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/include/clang/Basic/PartialDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/PartialDiagnostic.h?rev=85129&r1=85128&r2=85129&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/PartialDiagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/PartialDiagnostic.h Mon Oct 26 12:01:32 2009
@@ -109,7 +109,7 @@
// Add all arguments.
for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
- (Diagnostic::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
+ (Diagnostic::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
}
// Add all ranges.
@@ -129,13 +129,25 @@
PD.AddTaggedVal(I, Diagnostic::ak_uint);
return PD;
}
-
+
+ friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
+ int I) {
+ PD.AddTaggedVal(I, Diagnostic::ak_sint);
+ return PD;
+ }
+
+ friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
+ const char *S) {
+ PD.AddTaggedVal(reinterpret_cast<intptr_t>(S), Diagnostic::ak_c_string);
+ return PD;
+ }
+
friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
const SourceRange &R) {
PD.AddSourceRange(R);
return PD;
}
-
+
friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
DeclarationName N);
};
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=85129&r1=85128&r2=85129&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Oct 26 12:01:32 2009
@@ -5419,6 +5419,23 @@
return Opc >= BinaryOperator::LT && Opc <= BinaryOperator::NE;
}
+static void SuggestParentheses(Sema &Self, SourceLocation Loc,
+ const PartialDiagnostic &PD,
+ SourceRange ParenRange)
+{
+ SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd());
+ if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
+ // We can't display the parentheses, so just dig the
+ // warning/error and return.
+ Self.Diag(Loc, PD);
+ return;
+ }
+
+ Self.Diag(Loc, PD)
+ << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(")
+ << CodeModificationHint::CreateInsertion(EndLoc, ")");
+}
+
static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc,
SourceLocation OpLoc,Expr *lhs,Expr *rhs){
typedef BinaryOperator::Opcode Opcode;
@@ -5439,15 +5456,21 @@
return;
if (IsEqOrRel(lhsopc))
- Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel)
- << SourceRange(lhs->getLocStart(), OpLoc)
- << BinaryOperator::getOpcodeStr(Opc)
- << BinaryOperator::getOpcodeStr(static_cast<Opcode>(lhsopc));
+ SuggestParentheses(Self, OpLoc,
+ PDiag(diag::warn_precedence_bitwise_rel)
+ << SourceRange(lhs->getLocStart(), OpLoc)
+ << BinaryOperator::getOpcodeStr(Opc)
+ << BinaryOperator::getOpcodeStr(static_cast<Opcode>(lhsopc)),
+ SourceRange(cast<BinaryOperator>(lhs)->getRHS()->getLocStart(),
+ rhs->getLocEnd()));
else if (IsEqOrRel(rhsopc))
- Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel)
- << SourceRange(OpLoc, rhs->getLocEnd())
- << BinaryOperator::getOpcodeStr(Opc)
- << BinaryOperator::getOpcodeStr(static_cast<Opcode>(rhsopc));
+ SuggestParentheses(Self, OpLoc,
+ PDiag(diag::warn_precedence_bitwise_rel)
+ << SourceRange(OpLoc, rhs->getLocEnd())
+ << BinaryOperator::getOpcodeStr(Opc)
+ << BinaryOperator::getOpcodeStr(static_cast<Opcode>(rhsopc)),
+ SourceRange(lhs->getLocEnd(),
+ cast<BinaryOperator>(rhs)->getLHS()->getLocStart()));
}
/// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky
More information about the cfe-commits
mailing list