forked from OSchip/llvm-project
Revert "Add support for named values in the parser."
This was submitted before it was ready. This reverts commit 62060a01e095cf35eb9ca42a333752d12714f35c. llvm-svn: 205533
This commit is contained in:
parent
cabf0f41e0
commit
5548eadb1c
|
@ -18,14 +18,13 @@
|
||||||
///
|
///
|
||||||
/// \code
|
/// \code
|
||||||
/// Grammar for the expressions supported:
|
/// Grammar for the expressions supported:
|
||||||
/// <Expression> := <Literal> | <NamedValue> | <MatcherExpression>
|
/// <Expression> := <Literal> | <MatcherExpression>
|
||||||
/// <Literal> := <StringLiteral> | <Unsigned>
|
/// <Literal> := <StringLiteral> | <Unsigned>
|
||||||
/// <StringLiteral> := "quoted string"
|
/// <StringLiteral> := "quoted string"
|
||||||
/// <Unsigned> := [0-9]+
|
/// <Unsigned> := [0-9]+
|
||||||
/// <NamedValue> := <Identifier>
|
/// <MatcherExpression> := <MatcherName>(<ArgumentList>) |
|
||||||
/// <MatcherExpression> := <Identifier>(<ArgumentList>) |
|
/// <MatcherName>(<ArgumentList>).bind(<StringLiteral>)
|
||||||
/// <Identifier>(<ArgumentList>).bind(<StringLiteral>)
|
/// <MatcherName> := [a-zA-Z]+
|
||||||
/// <Identifier> := [a-zA-Z]+
|
|
||||||
/// <ArgumentList> := <Expression> | <Expression>,<ArgumentList>
|
/// <ArgumentList> := <Expression> | <Expression>,<ArgumentList>
|
||||||
/// \endcode
|
/// \endcode
|
||||||
///
|
///
|
||||||
|
@ -63,19 +62,6 @@ public:
|
||||||
public:
|
public:
|
||||||
virtual ~Sema();
|
virtual ~Sema();
|
||||||
|
|
||||||
/// \brief Lookup a value by name.
|
|
||||||
///
|
|
||||||
/// This can be used in the Sema layer to declare known constants or to
|
|
||||||
/// allow to split an expression in pieces.
|
|
||||||
///
|
|
||||||
/// \param Name The name of the value to lookup.
|
|
||||||
///
|
|
||||||
/// \return The named value. It could be any type that VariantValue
|
|
||||||
/// supports. A 'nothing' value means that the name is not recognized.
|
|
||||||
virtual VariantValue getNamedValue(StringRef Name) {
|
|
||||||
return VariantValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Process a matcher expression.
|
/// \brief Process a matcher expression.
|
||||||
///
|
///
|
||||||
/// All the arguments passed here have already been processed.
|
/// All the arguments passed here have already been processed.
|
||||||
|
@ -114,21 +100,6 @@ public:
|
||||||
Diagnostics *Error) = 0;
|
Diagnostics *Error) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief Sema implementation that uses the matcher registry to process the
|
|
||||||
/// tokens.
|
|
||||||
class RegistrySema : public Parser::Sema {
|
|
||||||
public:
|
|
||||||
virtual ~RegistrySema();
|
|
||||||
llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
|
|
||||||
const SourceRange &NameRange,
|
|
||||||
Diagnostics *Error) override;
|
|
||||||
VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
|
|
||||||
const SourceRange &NameRange,
|
|
||||||
StringRef BindID,
|
|
||||||
ArrayRef<ParserValue> Args,
|
|
||||||
Diagnostics *Error) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// \brief Parse a matcher expression, creating matchers from the registry.
|
/// \brief Parse a matcher expression, creating matchers from the registry.
|
||||||
///
|
///
|
||||||
/// This overload creates matchers calling directly into the registry. If the
|
/// This overload creates matchers calling directly into the registry. If the
|
||||||
|
|
|
@ -78,8 +78,7 @@ public:
|
||||||
/// \brief Clones the provided matchers.
|
/// \brief Clones the provided matchers.
|
||||||
///
|
///
|
||||||
/// They should be the result of a polymorphic matcher.
|
/// They should be the result of a polymorphic matcher.
|
||||||
static VariantMatcher
|
static VariantMatcher PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers);
|
||||||
PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers);
|
|
||||||
|
|
||||||
/// \brief Creates a 'variadic' operator matcher.
|
/// \brief Creates a 'variadic' operator matcher.
|
||||||
///
|
///
|
||||||
|
@ -209,9 +208,6 @@ public:
|
||||||
VariantValue(const std::string &String);
|
VariantValue(const std::string &String);
|
||||||
VariantValue(const VariantMatcher &Matchers);
|
VariantValue(const VariantMatcher &Matchers);
|
||||||
|
|
||||||
/// \brief Returns true iff this is an empty value.
|
|
||||||
bool isNothing() const { return Type == VT_Nothing; }
|
|
||||||
|
|
||||||
/// \brief Unsigned value functions.
|
/// \brief Unsigned value functions.
|
||||||
bool isUnsigned() const;
|
bool isUnsigned() const;
|
||||||
unsigned getUnsigned() const;
|
unsigned getUnsigned() const;
|
||||||
|
|
|
@ -424,18 +424,8 @@ bool Parser::parseExpressionImpl(VariantValue *Value) {
|
||||||
*Value = Tokenizer->consumeNextToken().Value;
|
*Value = Tokenizer->consumeNextToken().Value;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case TokenInfo::TK_Ident: {
|
case TokenInfo::TK_Ident:
|
||||||
// Identifier could be a name known by Sema as a named value.
|
|
||||||
const VariantValue NamedValue =
|
|
||||||
S->getNamedValue(Tokenizer->peekNextToken().Text);
|
|
||||||
if (!NamedValue.isNothing()) {
|
|
||||||
Tokenizer->consumeNextToken(); // Actually consume it.
|
|
||||||
*Value = NamedValue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// Fallback to full matcher parsing.
|
|
||||||
return parseMatcherExpressionImpl(Value);
|
return parseMatcherExpressionImpl(Value);
|
||||||
}
|
|
||||||
|
|
||||||
case TokenInfo::TK_CodeCompletion:
|
case TokenInfo::TK_CodeCompletion:
|
||||||
addExpressionCompletions();
|
addExpressionCompletions();
|
||||||
|
@ -467,23 +457,27 @@ Parser::Parser(CodeTokenizer *Tokenizer, Sema *S,
|
||||||
Diagnostics *Error)
|
Diagnostics *Error)
|
||||||
: Tokenizer(Tokenizer), S(S), Error(Error) {}
|
: Tokenizer(Tokenizer), S(S), Error(Error) {}
|
||||||
|
|
||||||
Parser::RegistrySema::~RegistrySema() {}
|
class RegistrySema : public Parser::Sema {
|
||||||
|
public:
|
||||||
llvm::Optional<MatcherCtor> Parser::RegistrySema::lookupMatcherCtor(
|
virtual ~RegistrySema() {}
|
||||||
StringRef MatcherName, const SourceRange &NameRange, Diagnostics *Error) {
|
llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
|
||||||
return Registry::lookupMatcherCtor(MatcherName, NameRange, Error);
|
const SourceRange &NameRange,
|
||||||
}
|
Diagnostics *Error) {
|
||||||
|
return Registry::lookupMatcherCtor(MatcherName, NameRange, Error);
|
||||||
VariantMatcher Parser::RegistrySema::actOnMatcherExpression(
|
|
||||||
MatcherCtor Ctor, const SourceRange &NameRange, StringRef BindID,
|
|
||||||
ArrayRef<ParserValue> Args, Diagnostics *Error) {
|
|
||||||
if (BindID.empty()) {
|
|
||||||
return Registry::constructMatcher(Ctor, NameRange, Args, Error);
|
|
||||||
} else {
|
|
||||||
return Registry::constructBoundMatcher(Ctor, NameRange, BindID, Args,
|
|
||||||
Error);
|
|
||||||
}
|
}
|
||||||
}
|
VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
|
||||||
|
const SourceRange &NameRange,
|
||||||
|
StringRef BindID,
|
||||||
|
ArrayRef<ParserValue> Args,
|
||||||
|
Diagnostics *Error) {
|
||||||
|
if (BindID.empty()) {
|
||||||
|
return Registry::constructMatcher(Ctor, NameRange, Args, Error);
|
||||||
|
} else {
|
||||||
|
return Registry::constructBoundMatcher(Ctor, NameRange, BindID, Args,
|
||||||
|
Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
bool Parser::parseExpression(StringRef Code, VariantValue *Value,
|
bool Parser::parseExpression(StringRef Code, VariantValue *Value,
|
||||||
Diagnostics *Error) {
|
Diagnostics *Error) {
|
||||||
|
|
|
@ -175,29 +175,6 @@ TEST(ParserTest, FullParserTest) {
|
||||||
EXPECT_TRUE(matches("void f(int a, int x);", M));
|
EXPECT_TRUE(matches("void f(int a, int x);", M));
|
||||||
EXPECT_FALSE(matches("void f(int x, int a);", M));
|
EXPECT_FALSE(matches("void f(int x, int a);", M));
|
||||||
|
|
||||||
// Test named values.
|
|
||||||
struct NamedSema : public Parser::RegistrySema {
|
|
||||||
public:
|
|
||||||
virtual VariantValue getNamedValue(StringRef Name) {
|
|
||||||
if (Name == "nameX")
|
|
||||||
return std::string("x");
|
|
||||||
if (Name == "param0")
|
|
||||||
return VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
|
|
||||||
return VariantValue();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
NamedSema Sema;
|
|
||||||
llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
|
|
||||||
Parser::parseMatcherExpression(
|
|
||||||
"functionDecl(param0, hasParameter(1, hasName(nameX)))", &Sema,
|
|
||||||
&Error));
|
|
||||||
EXPECT_EQ("", Error.toStringFull());
|
|
||||||
M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();
|
|
||||||
|
|
||||||
EXPECT_TRUE(matches("void f(int a, int x);", M));
|
|
||||||
EXPECT_FALSE(matches("void f(int x, int a);", M));
|
|
||||||
|
|
||||||
|
|
||||||
EXPECT_TRUE(!Parser::parseMatcherExpression(
|
EXPECT_TRUE(!Parser::parseMatcherExpression(
|
||||||
"hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
|
"hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
|
||||||
&Error).hasValue());
|
&Error).hasValue());
|
||||||
|
|
|
@ -26,7 +26,6 @@ TEST(VariantValueTest, Unsigned) {
|
||||||
EXPECT_TRUE(Value.isUnsigned());
|
EXPECT_TRUE(Value.isUnsigned());
|
||||||
EXPECT_EQ(kUnsigned, Value.getUnsigned());
|
EXPECT_EQ(kUnsigned, Value.getUnsigned());
|
||||||
|
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isString());
|
EXPECT_FALSE(Value.isString());
|
||||||
EXPECT_FALSE(Value.isMatcher());
|
EXPECT_FALSE(Value.isMatcher());
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,6 @@ TEST(VariantValueTest, String) {
|
||||||
EXPECT_EQ(kString, Value.getString());
|
EXPECT_EQ(kString, Value.getString());
|
||||||
EXPECT_EQ("String", Value.getTypeAsString());
|
EXPECT_EQ("String", Value.getTypeAsString());
|
||||||
|
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isUnsigned());
|
EXPECT_FALSE(Value.isUnsigned());
|
||||||
EXPECT_FALSE(Value.isMatcher());
|
EXPECT_FALSE(Value.isMatcher());
|
||||||
}
|
}
|
||||||
|
@ -47,7 +45,6 @@ TEST(VariantValueTest, String) {
|
||||||
TEST(VariantValueTest, DynTypedMatcher) {
|
TEST(VariantValueTest, DynTypedMatcher) {
|
||||||
VariantValue Value = VariantMatcher::SingleMatcher(stmt());
|
VariantValue Value = VariantMatcher::SingleMatcher(stmt());
|
||||||
|
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isUnsigned());
|
EXPECT_FALSE(Value.isUnsigned());
|
||||||
EXPECT_FALSE(Value.isString());
|
EXPECT_FALSE(Value.isString());
|
||||||
|
|
||||||
|
@ -77,13 +74,11 @@ TEST(VariantValueTest, Assignment) {
|
||||||
VariantValue Value = std::string("A");
|
VariantValue Value = std::string("A");
|
||||||
EXPECT_TRUE(Value.isString());
|
EXPECT_TRUE(Value.isString());
|
||||||
EXPECT_EQ("A", Value.getString());
|
EXPECT_EQ("A", Value.getString());
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isUnsigned());
|
EXPECT_FALSE(Value.isUnsigned());
|
||||||
EXPECT_FALSE(Value.isMatcher());
|
EXPECT_FALSE(Value.isMatcher());
|
||||||
EXPECT_EQ("String", Value.getTypeAsString());
|
EXPECT_EQ("String", Value.getTypeAsString());
|
||||||
|
|
||||||
Value = VariantMatcher::SingleMatcher(recordDecl());
|
Value = VariantMatcher::SingleMatcher(recordDecl());
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isUnsigned());
|
EXPECT_FALSE(Value.isUnsigned());
|
||||||
EXPECT_FALSE(Value.isString());
|
EXPECT_FALSE(Value.isString());
|
||||||
EXPECT_TRUE(Value.isMatcher());
|
EXPECT_TRUE(Value.isMatcher());
|
||||||
|
@ -94,12 +89,10 @@ TEST(VariantValueTest, Assignment) {
|
||||||
Value = 17;
|
Value = 17;
|
||||||
EXPECT_TRUE(Value.isUnsigned());
|
EXPECT_TRUE(Value.isUnsigned());
|
||||||
EXPECT_EQ(17U, Value.getUnsigned());
|
EXPECT_EQ(17U, Value.getUnsigned());
|
||||||
EXPECT_FALSE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isMatcher());
|
EXPECT_FALSE(Value.isMatcher());
|
||||||
EXPECT_FALSE(Value.isString());
|
EXPECT_FALSE(Value.isString());
|
||||||
|
|
||||||
Value = VariantValue();
|
Value = VariantValue();
|
||||||
EXPECT_TRUE(Value.isNothing());
|
|
||||||
EXPECT_FALSE(Value.isUnsigned());
|
EXPECT_FALSE(Value.isUnsigned());
|
||||||
EXPECT_FALSE(Value.isString());
|
EXPECT_FALSE(Value.isString());
|
||||||
EXPECT_FALSE(Value.isMatcher());
|
EXPECT_FALSE(Value.isMatcher());
|
||||||
|
|
Loading…
Reference in New Issue