[clang-tools-extra] c454609 - [pseudo] Use a real language option in the parser.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue May 3 13:25:23 PDT 2022
Author: Haojian Wu
Date: 2022-05-03T22:24:56+02:00
New Revision: c4546091ed29763df3e4649327679fec5dcb5ff6
URL: https://github.com/llvm/llvm-project/commit/c4546091ed29763df3e4649327679fec5dcb5ff6
DIFF: https://github.com/llvm/llvm-project/commit/c4546091ed29763df3e4649327679fec5dcb5ff6.diff
LOG: [pseudo] Use a real language option in the parser.
Differential Revision: https://reviews.llvm.org/D124831
Added:
Modified:
clang-tools-extra/pseudo/include/clang-pseudo/Token.h
clang-tools-extra/pseudo/lib/Token.cpp
clang-tools-extra/pseudo/test/glr.cpp
clang-tools-extra/pseudo/tool/ClangPseudo.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/pseudo/include/clang-pseudo/Token.h b/clang-tools-extra/pseudo/include/clang-pseudo/Token.h
index 2bbd598736e6d..31ef5eca26a2b 100644
--- a/clang-tools-extra/pseudo/include/clang-pseudo/Token.h
+++ b/clang-tools-extra/pseudo/include/clang-pseudo/Token.h
@@ -29,6 +29,7 @@
#define CLANG_PSEUDO_TOKEN_H
#include "clang/Basic/LLVM.h"
+#include "clang/Basic/LangStandard.h"
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"
@@ -193,6 +194,10 @@ enum class LexFlags : uint8_t {
/// The text() of such tokens will contain the raw trigrah.
NeedsCleaning = 1 << 1,
};
+/// A generic lang options suitable for lexing/parsing a langage.
+clang::LangOptions genericLangOpts(
+ clang::Language = clang::Language::CXX,
+ clang::LangStandard::Kind = clang::LangStandard::lang_unspecified);
/// Derives a token stream by decoding escapes, interpreting raw_identifiers and
/// splitting the greatergreater token.
diff --git a/clang-tools-extra/pseudo/lib/Token.cpp b/clang-tools-extra/pseudo/lib/Token.cpp
index f07c1d1b272df..b58c8e4a862e2 100644
--- a/clang-tools-extra/pseudo/lib/Token.cpp
+++ b/clang-tools-extra/pseudo/lib/Token.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "clang-pseudo/Token.h"
+#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
@@ -92,6 +93,28 @@ void TokenStream::print(llvm::raw_ostream &OS) const {
OS << '\n';
}
+clang::LangOptions genericLangOpts(clang::Language Lang,
+ clang::LangStandard::Kind Standard) {
+ clang::LangOptions Opts;
+ std::vector<std::string> UnusedIncludes;
+ LangOptions::setLangDefaults(Opts, Lang, llvm::Triple(), UnusedIncludes,
+ Standard);
+
+ // Some options are "on by default", but e.g. at the driver level.
+ if (Opts.CPlusPlus)
+ Opts.CXXOperatorNames = true;
+ if (Opts.CPlusPlus20)
+ Opts.Coroutines = true;
+
+ // Some options are off by default, but define keywords we want to tolerate.
+ if (Opts.CPlusPlus)
+ Opts.MicrosoftExt = true; // kw__try, kw__finally
+ Opts.DeclSpecKeyword = true; // __declspec
+ Opts.WChar = true;
+
+ return Opts;
+}
+
TokenStream stripComments(const TokenStream &Input) {
TokenStream Out;
for (const Token &T : Input.tokens()) {
diff --git a/clang-tools-extra/pseudo/test/glr.cpp b/clang-tools-extra/pseudo/test/glr.cpp
index 8817462d7d83e..8f58f6f894e59 100644
--- a/clang-tools-extra/pseudo/test/glr.cpp
+++ b/clang-tools-extra/pseudo/test/glr.cpp
@@ -21,3 +21,15 @@ void foo() {
// CHECK-NEXT: │ └─ptr-declarator~IDENTIFIER := tok[7]
// CHECK-NEXT: └─; := tok[8]
}
+
+bool operator<();
+// CHECK: declaration~simple-declaration := decl-specifier-seq init-declarator-list ;
+// CHECK-NEXT: ├─decl-specifier-seq~BOOL
+// CHECK-NEXT: ├─init-declarator-list~noptr-declarator := noptr-declarator parameters-and-qualifiers
+// CHECK-NEXT: │ ├─noptr-declarator~operator-function-id := OPERATOR operator-name
+// CHECK-NEXT: │ │ ├─OPERATOR
+// CHECK-NEXT: │ │ └─operator-name~<
+// CHECK-NEXT: │ └─parameters-and-qualifiers := ( )
+// CHECK-NEXT: │ ├─(
+// CHECK-NEXT: │ └─)
+// CHECK-NEXT: └─;
diff --git a/clang-tools-extra/pseudo/tool/ClangPseudo.cpp b/clang-tools-extra/pseudo/tool/ClangPseudo.cpp
index 71ff5b0a637a2..e55b7006c7e4f 100644
--- a/clang-tools-extra/pseudo/tool/ClangPseudo.cpp
+++ b/clang-tools-extra/pseudo/tool/ClangPseudo.cpp
@@ -53,8 +53,7 @@ static std::string readOrDie(llvm::StringRef Path) {
int main(int argc, char *argv[]) {
llvm::cl::ParseCommandLineOptions(argc, argv, "");
- clang::LangOptions LangOpts; // FIXME: use real options.
- LangOpts.CPlusPlus = 1;
+ clang::LangOptions LangOpts = clang::pseudo::genericLangOpts();
std::string SourceText;
llvm::Optional<clang::pseudo::TokenStream> RawStream;
llvm::Optional<clang::pseudo::DirectiveTree> DirectiveStructure;
More information about the cfe-commits
mailing list