|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "clang/Analysis/FlowSensitive/Arena.h"
|
| 10 | +#include "clang/Analysis/FlowSensitive/Formula.h" |
10 | 11 | #include "clang/Analysis/FlowSensitive/Value.h"
|
| 12 | +#include "llvm/Support/Error.h" |
| 13 | +#include <string> |
11 | 14 |
|
12 | 15 | namespace clang::dataflow {
|
13 | 16 |
|
@@ -95,4 +98,96 @@ BoolValue &Arena::makeBoolValue(const Formula &F) {
|
95 | 98 | return *It->second;
|
96 | 99 | }
|
97 | 100 |
|
| 101 | +namespace { |
| 102 | +const Formula *parse(Arena &A, llvm::StringRef &In) { |
| 103 | + auto EatSpaces = [&] { In = In.ltrim(' '); }; |
| 104 | + EatSpaces(); |
| 105 | + |
| 106 | + if (In.consume_front("!")) { |
| 107 | + if (auto *Arg = parse(A, In)) |
| 108 | + return &A.makeNot(*Arg); |
| 109 | + return nullptr; |
| 110 | + } |
| 111 | + |
| 112 | + if (In.consume_front("(")) { |
| 113 | + auto *Arg1 = parse(A, In); |
| 114 | + if (!Arg1) |
| 115 | + return nullptr; |
| 116 | + |
| 117 | + EatSpaces(); |
| 118 | + decltype(&Arena::makeOr) Op; |
| 119 | + if (In.consume_front("|")) |
| 120 | + Op = &Arena::makeOr; |
| 121 | + else if (In.consume_front("&")) |
| 122 | + Op = &Arena::makeAnd; |
| 123 | + else if (In.consume_front("=>")) |
| 124 | + Op = &Arena::makeImplies; |
| 125 | + else if (In.consume_front("=")) |
| 126 | + Op = &Arena::makeEquals; |
| 127 | + else |
| 128 | + return nullptr; |
| 129 | + |
| 130 | + auto *Arg2 = parse(A, In); |
| 131 | + if (!Arg2) |
| 132 | + return nullptr; |
| 133 | + |
| 134 | + EatSpaces(); |
| 135 | + if (!In.consume_front(")")) |
| 136 | + return nullptr; |
| 137 | + |
| 138 | + return &(A.*Op)(*Arg1, *Arg2); |
| 139 | + } |
| 140 | + |
| 141 | + // For now, only support unnamed variables V0, V1 etc. |
| 142 | + // FIXME: parse e.g. "X" by allocating an atom and storing a name somewhere. |
| 143 | + if (In.consume_front("V")) { |
| 144 | + std::underlying_type_t<Atom> At; |
| 145 | + if (In.consumeInteger(10, At)) |
| 146 | + return nullptr; |
| 147 | + return &A.makeAtomRef(static_cast<Atom>(At)); |
| 148 | + } |
| 149 | + |
| 150 | + if (In.consume_front("true")) |
| 151 | + return &A.makeLiteral(true); |
| 152 | + if (In.consume_front("false")) |
| 153 | + return &A.makeLiteral(false); |
| 154 | + |
| 155 | + return nullptr; |
| 156 | +} |
| 157 | + |
| 158 | +class FormulaParseError : public llvm::ErrorInfo<FormulaParseError> { |
| 159 | + std::string Formula; |
| 160 | + unsigned Offset; |
| 161 | + |
| 162 | +public: |
| 163 | + static char ID; |
| 164 | + FormulaParseError(llvm::StringRef Formula, unsigned Offset) |
| 165 | + : Formula(Formula), Offset(Offset) {} |
| 166 | + |
| 167 | + void log(raw_ostream &OS) const override { |
| 168 | + OS << "bad formula at offset " << Offset << "\n"; |
| 169 | + OS << Formula << "\n"; |
| 170 | + OS.indent(Offset) << "^"; |
| 171 | + } |
| 172 | + |
| 173 | + std::error_code convertToErrorCode() const override { |
| 174 | + return std::make_error_code(std::errc::invalid_argument); |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +char FormulaParseError::ID = 0; |
| 179 | + |
| 180 | +} // namespace |
| 181 | + |
| 182 | +llvm::Expected<const Formula &> Arena::parseFormula(llvm::StringRef In) { |
| 183 | + llvm::StringRef Rest = In; |
| 184 | + auto *Result = parse(*this, Rest); |
| 185 | + if (!Result) // parse() hit something unparseable |
| 186 | + return llvm::make_error<FormulaParseError>(In, In.size() - Rest.size()); |
| 187 | + Rest = Rest.ltrim(); |
| 188 | + if (!Rest.empty()) // parse didn't consume all the input |
| 189 | + return llvm::make_error<FormulaParseError>(In, In.size() - Rest.size()); |
| 190 | + return *Result; |
| 191 | +} |
| 192 | + |
98 | 193 | } // namespace clang::dataflow
|
0 commit comments