summaryrefslogtreecommitdiffstats
path: root/clang/lib/StaticAnalyzer
diff options
context:
space:
mode:
authorGabor Horvath <xazax.hun@gmail.com>2017-10-30 17:06:42 +0000
committerGabor Horvath <xazax.hun@gmail.com>2017-10-30 17:06:42 +0000
commit4581f7497b41a90595a5a850c6a031b9cf60f50e (patch)
tree22bf7ce3e62fa4426b20eb4db3832f925001f0fd /clang/lib/StaticAnalyzer
parent0f4c5712b40e83b30e8ce248dcd06a73921cfbbf (diff)
downloadbcm5719-llvm-4581f7497b41a90595a5a850c6a031b9cf60f50e.tar.gz
bcm5719-llvm-4581f7497b41a90595a5a850c6a031b9cf60f50e.zip
[analyzer] Left shifting a negative value is undefined
The analyzer did not return an UndefVal in case a negative value was left shifted. I also altered the UndefResultChecker to emit a clear warning in this case. Differential Revision: https://reviews.llvm.org/D39423 llvm-svn: 316924
Diffstat (limited to 'clang/lib/StaticAnalyzer')
-rw-r--r--clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp4
-rw-r--r--clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp2
2 files changed, 6 insertions, 0 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
index 21e8b107c9e..172ce346f1b 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
@@ -137,6 +137,10 @@ void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
OS << " greater or equal to the width of type '"
<< B->getLHS()->getType().getAsString() << "'.";
+ } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
+ C.isNegative(B->getLHS())) {
+ OS << "The result of the left shift is undefined because the left "
+ "operand is negative";
} else {
OS << "The result of the '"
<< BinaryOperator::getOpcodeStr(B->getOpcode())
diff --git a/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp b/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
index ebbace4e33b..ec7a7e9e4b1 100644
--- a/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
@@ -225,6 +225,8 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
// test these conditions symbolically.
// FIXME: Expand these checks to include all undefined behavior.
+ if (V1.isSigned() && V1.isNegative())
+ return nullptr;
if (V2.isSigned() && V2.isNegative())
return nullptr;
OpenPOWER on IntegriCloud