[PATCH] D124831: [pseudo] Use a real language option in the parser.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 3 13:12:35 PDT 2022


hokein updated this revision to Diff 426811.
hokein added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124831/new/

https://reviews.llvm.org/D124831

Files:
  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


Index: clang-tools-extra/pseudo/tool/ClangPseudo.cpp
===================================================================
--- clang-tools-extra/pseudo/tool/ClangPseudo.cpp
+++ clang-tools-extra/pseudo/tool/ClangPseudo.cpp
@@ -53,8 +53,7 @@
 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;
Index: clang-tools-extra/pseudo/test/glr.cpp
===================================================================
--- clang-tools-extra/pseudo/test/glr.cpp
+++ clang-tools-extra/pseudo/test/glr.cpp
@@ -21,3 +21,15 @@
 // 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: └─;
Index: clang-tools-extra/pseudo/lib/Token.cpp
===================================================================
--- clang-tools-extra/pseudo/lib/Token.cpp
+++ 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 @@
     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()) {
Index: clang-tools-extra/pseudo/include/clang-pseudo/Token.h
===================================================================
--- clang-tools-extra/pseudo/include/clang-pseudo/Token.h
+++ 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 @@
   /// 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124831.426811.patch
Type: text/x-patch
Size: 3875 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220503/95af545a/attachment.bin>


More information about the cfe-commits mailing list