Reland "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"

Restore getFormattingLangOpts().
This commit is contained in:
Owen Pan
2024-02-11 21:54:32 -08:00
parent 5da801386c
commit 32e65b0b8a
6 changed files with 15 additions and 17 deletions

View File

@@ -13,11 +13,7 @@
//===----------------------------------------------------------------------===//
#include "FormatTokenLexer.h"
#include "FormatToken.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Regex.h"
#include "TokenAnalyzer.h"
namespace clang {
namespace format {
@@ -28,12 +24,12 @@ FormatTokenLexer::FormatTokenLexer(
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
IdentifierTable &IdentTable)
: FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}),
Column(Column), TrailingWhitespace(0),
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
Column(Column), TrailingWhitespace(0), 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);
@@ -1442,7 +1438,7 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
void FormatTokenLexer::resetLexer(unsigned Offset) {
StringRef Buffer = SourceMgr.getBufferData(ID);
LangOpts = getFormattingLangOpts(Style);
assert(LangOpts.CPlusPlus);
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
Lex->SetKeepWhitespaceMode(true);

View File

@@ -17,14 +17,9 @@
#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"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Regex.h"
#include <stack>
@@ -120,7 +115,6 @@ private:
unsigned Column;
unsigned TrailingWhitespace;
std::unique_ptr<Lexer> Lex;
LangOptions LangOpts;
const SourceManager &SourceMgr;
FileID ID;
const FormatStyle &Style;

View File

@@ -79,7 +79,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
AffectedRangeManager AffectedRangeMgr(SourceMgr, Env.getCharRanges());
const auto ID = Env.getFileID();
const auto LangOpts = getFormattingLangOpts(Style);
assert(LangOpts.CPlusPlus);
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
Lex.SetCommentRetentionState(true);

View File

@@ -35,6 +35,8 @@
namespace clang {
namespace format {
LangOptions LangOpts;
// 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 {
@@ -99,9 +101,11 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
std::pair<tooling::Replacements, unsigned>
TokenAnalyzer::process(bool SkipAnnotation) {
LangOpts = getFormattingLangOpts(Style);
tooling::Replacements Result;
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
IdentifierTable IdentTable(getFormattingLangOpts(Style));
IdentifierTable IdentTable(LangOpts);
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
Env.getFirstStartColumn(), Style, Encoding, Allocator,
IdentTable);

View File

@@ -34,6 +34,8 @@
namespace clang {
namespace format {
extern LangOptions LangOpts;
class Environment {
public:
// This sets up an virtual file system with file \p FileName containing the

View File

@@ -61,7 +61,9 @@ public:
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
FormatStyle Style = getLLVMStyle())
: Allocator(Allocator), Buffers(Buffers), Style(Style),
SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
SourceMgr("test.cpp", ""), IdentTable(LangOpts) {
assert(LangOpts.CPlusPlus);
}
TokenList lex(llvm::StringRef Code) {
FormatTokenLexer Lex = getNewLexer(Code);