Revert "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"
This reverts commit 03f571995b4f0c260254955afd16ec44d0764794. We can't hide getFormattingLangOpts() as it's used by other tools.
This commit is contained in:
parent
03f571995b
commit
3dc8ef677d
@ -14,6 +14,7 @@
|
||||
#ifndef LLVM_CLANG_FORMAT_FORMAT_H
|
||||
#define LLVM_CLANG_FORMAT_FORMAT_H
|
||||
|
||||
#include "clang/Basic/LangOptions.h"
|
||||
#include "clang/Tooling/Core/Replacement.h"
|
||||
#include "clang/Tooling/Inclusions/IncludeStyle.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
@ -5178,6 +5179,11 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
|
||||
ArrayRef<tooling::Range> Ranges,
|
||||
StringRef FileName = "<stdin>");
|
||||
|
||||
/// Returns the ``LangOpts`` that the formatter expects you to set.
|
||||
///
|
||||
/// \param Style determines specific settings for lexing mode.
|
||||
LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle());
|
||||
|
||||
/// Description to be used for help text for a ``llvm::cl`` option for
|
||||
/// specifying format style. The description is closely related to the operation
|
||||
/// of ``getStyle()``.
|
||||
|
||||
@ -3823,6 +3823,36 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
|
||||
return UsingDeclarationsSorter(*Env, Style).process().first;
|
||||
}
|
||||
|
||||
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
|
||||
LangOptions LangOpts;
|
||||
|
||||
FormatStyle::LanguageStandard LexingStd = Style.Standard;
|
||||
if (LexingStd == FormatStyle::LS_Auto)
|
||||
LexingStd = FormatStyle::LS_Latest;
|
||||
if (LexingStd == FormatStyle::LS_Latest)
|
||||
LexingStd = FormatStyle::LS_Cpp20;
|
||||
LangOpts.CPlusPlus = 1;
|
||||
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
|
||||
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
|
||||
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
|
||||
LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
|
||||
LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
|
||||
// Turning on digraphs in standards before C++0x is error-prone, because e.g.
|
||||
// the sequence "<::" will be unconditionally treated as "[:".
|
||||
// Cf. Lexer::LexTokenInternal.
|
||||
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
|
||||
|
||||
LangOpts.LineComment = 1;
|
||||
bool AlternativeOperators = Style.isCpp();
|
||||
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
|
||||
LangOpts.Bool = 1;
|
||||
LangOpts.ObjC = 1;
|
||||
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
|
||||
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
|
||||
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
|
||||
return LangOpts;
|
||||
}
|
||||
|
||||
const char *StyleOptionHelpDescription =
|
||||
"Set coding style. <string> can be:\n"
|
||||
"1. A preset: LLVM, GNU, Google, Chromium, Microsoft,\n"
|
||||
|
||||
@ -13,7 +13,11 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "FormatTokenLexer.h"
|
||||
#include "TokenAnalyzer.h"
|
||||
#include "FormatToken.h"
|
||||
#include "clang/Basic/SourceLocation.h"
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
#include "clang/Format/Format.h"
|
||||
#include "llvm/Support/Regex.h"
|
||||
|
||||
namespace clang {
|
||||
namespace format {
|
||||
@ -24,12 +28,12 @@ FormatTokenLexer::FormatTokenLexer(
|
||||
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
|
||||
IdentifierTable &IdentTable)
|
||||
: FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}),
|
||||
Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
|
||||
Column(Column), TrailingWhitespace(0),
|
||||
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
|
||||
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
|
||||
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
|
||||
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
|
||||
MacroBlockEndRegex(Style.MacroBlockEnd) {
|
||||
assert(LangOpts.CPlusPlus);
|
||||
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
|
||||
Lex->SetKeepWhitespaceMode(true);
|
||||
|
||||
@ -1438,7 +1442,7 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
|
||||
|
||||
void FormatTokenLexer::resetLexer(unsigned Offset) {
|
||||
StringRef Buffer = SourceMgr.getBufferData(ID);
|
||||
assert(LangOpts.CPlusPlus);
|
||||
LangOpts = getFormattingLangOpts(Style);
|
||||
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
|
||||
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
|
||||
Lex->SetKeepWhitespaceMode(true);
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
#include "Encoding.h"
|
||||
#include "FormatToken.h"
|
||||
#include "clang/Basic/LangOptions.h"
|
||||
#include "clang/Basic/SourceLocation.h"
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
#include "clang/Format/Format.h"
|
||||
@ -119,6 +120,7 @@ private:
|
||||
unsigned Column;
|
||||
unsigned TrailingWhitespace;
|
||||
std::unique_ptr<Lexer> Lex;
|
||||
LangOptions LangOpts;
|
||||
const SourceManager &SourceMgr;
|
||||
FileID ID;
|
||||
const FormatStyle &Style;
|
||||
|
||||
@ -79,7 +79,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
|
||||
AffectedRangeManager AffectedRangeMgr(SourceMgr, Env.getCharRanges());
|
||||
|
||||
const auto ID = Env.getFileID();
|
||||
assert(LangOpts.CPlusPlus);
|
||||
const auto LangOpts = getFormattingLangOpts(Style);
|
||||
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
|
||||
Lex.SetCommentRetentionState(true);
|
||||
|
||||
|
||||
@ -35,38 +35,6 @@
|
||||
namespace clang {
|
||||
namespace format {
|
||||
|
||||
LangOptions LangOpts;
|
||||
|
||||
/// Sets `LangOpts` for the formatter.
|
||||
///
|
||||
/// \param `Style` determines specific settings for lexing mode.
|
||||
static void setFormattingLangOpts(const FormatStyle &Style) {
|
||||
FormatStyle::LanguageStandard LexingStd = Style.Standard;
|
||||
if (LexingStd == FormatStyle::LS_Auto)
|
||||
LexingStd = FormatStyle::LS_Latest;
|
||||
if (LexingStd == FormatStyle::LS_Latest)
|
||||
LexingStd = FormatStyle::LS_Cpp20;
|
||||
LangOpts.CPlusPlus = 1;
|
||||
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
|
||||
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
|
||||
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
|
||||
LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
|
||||
LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
|
||||
// Turning on digraphs in standards before C++0x is error-prone, because e.g.
|
||||
// the sequence "<::" will be unconditionally treated as "[:".
|
||||
// Cf. Lexer::LexTokenInternal.
|
||||
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
|
||||
|
||||
LangOpts.LineComment = 1;
|
||||
bool AlternativeOperators = Style.isCpp();
|
||||
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
|
||||
LangOpts.Bool = 1;
|
||||
LangOpts.ObjC = 1;
|
||||
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
|
||||
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
|
||||
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
|
||||
}
|
||||
|
||||
// FIXME: Instead of printing the diagnostic we should store it and have a
|
||||
// better way to return errors through the format APIs.
|
||||
class FatalDiagnosticConsumer : public DiagnosticConsumer {
|
||||
@ -131,11 +99,9 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
|
||||
|
||||
std::pair<tooling::Replacements, unsigned>
|
||||
TokenAnalyzer::process(bool SkipAnnotation) {
|
||||
setFormattingLangOpts(Style);
|
||||
|
||||
tooling::Replacements Result;
|
||||
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
|
||||
IdentifierTable IdentTable(LangOpts);
|
||||
IdentifierTable IdentTable(getFormattingLangOpts(Style));
|
||||
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
|
||||
Env.getFirstStartColumn(), Style, Encoding, Allocator,
|
||||
IdentTable);
|
||||
|
||||
@ -34,8 +34,6 @@
|
||||
namespace clang {
|
||||
namespace format {
|
||||
|
||||
extern LangOptions LangOpts;
|
||||
|
||||
class Environment {
|
||||
public:
|
||||
// This sets up an virtual file system with file \p FileName containing the
|
||||
|
||||
@ -61,9 +61,7 @@ public:
|
||||
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
|
||||
FormatStyle Style = getLLVMStyle())
|
||||
: Allocator(Allocator), Buffers(Buffers), Style(Style),
|
||||
SourceMgr("test.cpp", ""), IdentTable(LangOpts) {
|
||||
assert(LangOpts.CPlusPlus);
|
||||
}
|
||||
SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
|
||||
|
||||
TokenList lex(llvm::StringRef Code) {
|
||||
FormatTokenLexer Lex = getNewLexer(Code);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user