summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-operator-shift-overflow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/slang/slang-ir-operator-shift-overflow.cpp')
-rw-r--r--source/slang/slang-ir-operator-shift-overflow.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/source/slang/slang-ir-operator-shift-overflow.cpp b/source/slang/slang-ir-operator-shift-overflow.cpp
new file mode 100644
index 000000000..33ee53262
--- /dev/null
+++ b/source/slang/slang-ir-operator-shift-overflow.cpp
@@ -0,0 +1,70 @@
+// slang-ir-operator-shift-overflow.cpp
+#include "slang-ir-operator-shift-overflow.h"
+
+#include "../../slang.h"
+#include "slang-ir.h"
+#include "slang-ir-insts.h"
+#include "slang-ir-layout.h"
+
+namespace Slang {
+
+ class DiagnosticSink;
+ struct IRModule;
+
+ void checkForOperatorShiftOverflowRecursive(
+ IRInst* inst,
+ CompilerOptionSet& optionSet,
+ DiagnosticSink* sink)
+ {
+ if (auto code = as<IRGlobalValueWithCode>(inst))
+ {
+ for (auto block : code->getBlocks())
+ {
+ for (auto opInst : block->getChildren())
+ {
+ switch (opInst->getOp())
+ {
+ case kIROp_Lsh:
+ {
+ SLANG_ASSERT(opInst->getOperandCount() == 2);
+
+ IRInst* rhs = opInst->getOperand(1);
+ auto rhsLit = as<IRIntLit>(rhs);
+ if (!rhsLit)
+ continue;
+
+ IRInst* lhs = opInst->getOperand(0);
+ IRType* lhsType = lhs->getDataType();
+
+ IRSizeAndAlignment sizeAlignment;
+ if (SLANG_FAILED(getNaturalSizeAndAlignment(optionSet, lhsType, &sizeAlignment)))
+ continue;
+
+ IRIntegerValue shiftAmount = rhsLit->getValue();
+ if (sizeAlignment.size * 8 <= shiftAmount)
+ {
+ sink->diagnose(opInst, Diagnostics::operatorShiftLeftOverflow, lhsType, shiftAmount);
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ for (auto childInst : inst->getChildren())
+ {
+ checkForOperatorShiftOverflowRecursive(childInst, optionSet, sink);
+ }
+ }
+
+ void checkForOperatorShiftOverflow(
+ IRModule* module,
+ CompilerOptionSet& optionSet,
+ DiagnosticSink* sink)
+ {
+ // Look for `operator<<` instructions
+ checkForOperatorShiftOverflowRecursive(module->getModuleInst(), optionSet, sink);
+ }
+
+}