2013-11-08 08:08:23 +08:00
|
|
|
//===--- Query.h - clang-query ----------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-08 08:08:23 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
|
|
|
|
|
2018-10-25 04:33:55 +08:00
|
|
|
#include "QuerySession.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include <string>
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace query {
|
|
|
|
|
2018-10-25 04:33:45 +08:00
|
|
|
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST };
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
enum QueryKind {
|
|
|
|
QK_Invalid,
|
|
|
|
QK_NoOp,
|
|
|
|
QK_Help,
|
2014-04-23 22:04:52 +08:00
|
|
|
QK_Let,
|
2013-11-08 08:08:23 +08:00
|
|
|
QK_Match,
|
|
|
|
QK_SetBool,
|
2014-04-23 22:04:52 +08:00
|
|
|
QK_SetOutputKind,
|
2018-10-30 02:59:56 +08:00
|
|
|
QK_EnableOutputKind,
|
|
|
|
QK_DisableOutputKind,
|
2015-08-06 19:56:57 +08:00
|
|
|
QK_Quit
|
2013-11-08 08:08:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class QuerySession;
|
|
|
|
|
|
|
|
struct Query : llvm::RefCountedBase<Query> {
|
|
|
|
Query(QueryKind Kind) : Kind(Kind) {}
|
|
|
|
virtual ~Query();
|
|
|
|
|
|
|
|
/// Perform the query on \p QS and print output to \p OS.
|
|
|
|
///
|
|
|
|
/// \return false if an error occurs, otherwise return true.
|
|
|
|
virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
|
|
|
|
|
|
|
|
const QueryKind Kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
|
|
|
|
|
|
|
|
/// Any query which resulted in a parse error. The error message is in ErrStr.
|
|
|
|
struct InvalidQuery : Query {
|
|
|
|
InvalidQuery(const Twine &ErrStr) : Query(QK_Invalid), ErrStr(ErrStr.str()) {}
|
2014-03-02 18:20:11 +08:00
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
std::string ErrStr;
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_Invalid; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// No-op query (i.e. a blank line).
|
|
|
|
struct NoOpQuery : Query {
|
|
|
|
NoOpQuery() : Query(QK_NoOp) {}
|
2014-03-02 18:20:11 +08:00
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Query for "help".
|
|
|
|
struct HelpQuery : Query {
|
|
|
|
HelpQuery() : Query(QK_Help) {}
|
2014-03-02 18:20:11 +08:00
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
|
|
|
|
};
|
|
|
|
|
2015-08-06 19:56:57 +08:00
|
|
|
/// Query for "quit".
|
|
|
|
struct QuitQuery : Query {
|
|
|
|
QuitQuery() : Query(QK_Quit) {}
|
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
|
|
|
|
};
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
/// Query for "match MATCHER".
|
|
|
|
struct MatchQuery : Query {
|
2018-10-20 17:13:59 +08:00
|
|
|
MatchQuery(StringRef Source,
|
|
|
|
const ast_matchers::dynamic::DynTypedMatcher &Matcher)
|
|
|
|
: Query(QK_Match), Matcher(Matcher), Source(Source) {}
|
2014-03-02 18:20:11 +08:00
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
ast_matchers::dynamic::DynTypedMatcher Matcher;
|
|
|
|
|
2018-10-20 17:13:59 +08:00
|
|
|
StringRef Source;
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
|
|
|
|
};
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
struct LetQuery : Query {
|
|
|
|
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
|
|
|
|
: Query(QK_Let), Name(Name), Value(Value) {}
|
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
|
|
|
|
|
|
|
|
std::string Name;
|
|
|
|
ast_matchers::dynamic::VariantValue Value;
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
|
|
|
|
};
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
template <typename T> struct SetQueryKind {};
|
|
|
|
|
|
|
|
template <> struct SetQueryKind<bool> {
|
|
|
|
static const QueryKind value = QK_SetBool;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct SetQueryKind<OutputKind> {
|
|
|
|
static const QueryKind value = QK_SetOutputKind;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Query for "set VAR VALUE".
|
|
|
|
template <typename T> struct SetQuery : Query {
|
|
|
|
SetQuery(T QuerySession::*Var, T Value)
|
|
|
|
: Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
|
2014-03-02 18:20:11 +08:00
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
|
2013-11-08 08:08:23 +08:00
|
|
|
QS.*Var = Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) {
|
|
|
|
return Q->Kind == SetQueryKind<T>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T QuerySession::*Var;
|
|
|
|
T Value;
|
|
|
|
};
|
|
|
|
|
2018-10-25 04:33:55 +08:00
|
|
|
// Implements the exclusive 'set output dump|diag|print' options.
|
|
|
|
struct SetExclusiveOutputQuery : Query {
|
|
|
|
SetExclusiveOutputQuery(bool QuerySession::*Var)
|
|
|
|
: Query(QK_SetOutputKind), Var(Var) {}
|
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
|
|
|
|
QS.DiagOutput = false;
|
|
|
|
QS.DetailedASTOutput = false;
|
|
|
|
QS.PrintOutput = false;
|
|
|
|
QS.*Var = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
|
|
|
|
|
|
|
|
bool QuerySession::*Var;
|
|
|
|
};
|
|
|
|
|
2018-10-30 02:59:56 +08:00
|
|
|
// Implements the non-exclusive 'set output dump|diag|print' options.
|
|
|
|
struct SetNonExclusiveOutputQuery : Query {
|
|
|
|
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var,
|
|
|
|
bool Value)
|
|
|
|
: Query(Kind), Var(Var), Value(Value) {}
|
|
|
|
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
|
|
|
|
QS.*Var = Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuerySession::*Var;
|
|
|
|
bool Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnableOutputQuery : SetNonExclusiveOutputQuery {
|
|
|
|
EnableOutputQuery(bool QuerySession::*Var)
|
|
|
|
: SetNonExclusiveOutputQuery(QK_EnableOutputKind, Var, true) {}
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DisableOutputQuery : SetNonExclusiveOutputQuery {
|
|
|
|
DisableOutputQuery(bool QuerySession::*Var)
|
|
|
|
: SetNonExclusiveOutputQuery(QK_DisableOutputKind, Var, false) {}
|
|
|
|
|
|
|
|
static bool classof(const Query *Q) {
|
|
|
|
return Q->Kind == QK_DisableOutputKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
} // namespace query
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|