diff --git a/clang-tools-extra/pseudo/include/clang-pseudo/Token.h b/clang-tools-extra/pseudo/include/clang-pseudo/Token.h --- 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 @@ /// 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 --- 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 @@ OS << '\n'; } +clang::LangOptions genericLangOpts(clang::Language Lang, + clang::LangStandard::Kind Standard) { + clang::LangOptions Opts; + std::vector 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 --- a/clang-tools-extra/pseudo/test/glr.cpp +++ b/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: └─; diff --git a/clang-tools-extra/pseudo/tool/ClangPseudo.cpp b/clang-tools-extra/pseudo/tool/ClangPseudo.cpp --- a/clang-tools-extra/pseudo/tool/ClangPseudo.cpp +++ b/clang-tools-extra/pseudo/tool/ClangPseudo.cpp @@ -53,14 +53,12 @@ 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(); llvm::Optional RawStream; llvm::Optional DirectiveStructure; llvm::Optional ParseableStream; if (Source.getNumOccurrences()) { std::string Text = readOrDie(Source); - clang::LangOptions LangOpts; // FIXME: use real options. RawStream = clang::pseudo::lex(Text, LangOpts); DirectiveStructure = clang::pseudo::DirectiveTree::parse(*RawStream); clang::pseudo::chooseConditionalBranches(*DirectiveStructure, *RawStream);