Skip to content

Commit 32e65b0

Browse files
committed
Reland "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"
Restore getFormattingLangOpts().
1 parent 5da8013 commit 32e65b0

File tree

6 files changed

+15
-17
lines changed

6 files changed

+15
-17
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "FormatTokenLexer.h"
16-
#include "FormatToken.h"
17-
#include "clang/Basic/SourceLocation.h"
18-
#include "clang/Basic/SourceManager.h"
19-
#include "clang/Format/Format.h"
20-
#include "llvm/Support/Regex.h"
16+
#include "TokenAnalyzer.h"
2117

2218
namespace clang {
2319
namespace format {
@@ -28,12 +24,12 @@ FormatTokenLexer::FormatTokenLexer(
2824
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
2925
IdentifierTable &IdentTable)
3026
: FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}),
31-
Column(Column), TrailingWhitespace(0),
32-
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
27+
Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
3328
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
3429
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
3530
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
3631
MacroBlockEndRegex(Style.MacroBlockEnd) {
32+
assert(LangOpts.CPlusPlus);
3733
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
3834
Lex->SetKeepWhitespaceMode(true);
3935

@@ -1442,7 +1438,7 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
14421438

14431439
void FormatTokenLexer::resetLexer(unsigned Offset) {
14441440
StringRef Buffer = SourceMgr.getBufferData(ID);
1445-
LangOpts = getFormattingLangOpts(Style);
1441+
assert(LangOpts.CPlusPlus);
14461442
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
14471443
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
14481444
Lex->SetKeepWhitespaceMode(true);

clang/lib/Format/FormatTokenLexer.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@
1717

1818
#include "Encoding.h"
1919
#include "FormatToken.h"
20-
#include "clang/Basic/LangOptions.h"
21-
#include "clang/Basic/SourceLocation.h"
22-
#include "clang/Basic/SourceManager.h"
23-
#include "clang/Format/Format.h"
2420
#include "llvm/ADT/MapVector.h"
2521
#include "llvm/ADT/SmallPtrSet.h"
2622
#include "llvm/ADT/StringSet.h"
27-
#include "llvm/Support/Regex.h"
2823

2924
#include <stack>
3025

@@ -120,7 +115,6 @@ class FormatTokenLexer {
120115
unsigned Column;
121116
unsigned TrailingWhitespace;
122117
std::unique_ptr<Lexer> Lex;
123-
LangOptions LangOpts;
124118
const SourceManager &SourceMgr;
125119
FileID ID;
126120
const FormatStyle &Style;

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
7979
AffectedRangeManager AffectedRangeMgr(SourceMgr, Env.getCharRanges());
8080

8181
const auto ID = Env.getFileID();
82-
const auto LangOpts = getFormattingLangOpts(Style);
82+
assert(LangOpts.CPlusPlus);
8383
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
8484
Lex.SetCommentRetentionState(true);
8585

clang/lib/Format/TokenAnalyzer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
namespace clang {
3636
namespace format {
3737

38+
LangOptions LangOpts;
39+
3840
// FIXME: Instead of printing the diagnostic we should store it and have a
3941
// better way to return errors through the format APIs.
4042
class FatalDiagnosticConsumer : public DiagnosticConsumer {
@@ -99,9 +101,11 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
99101

100102
std::pair<tooling::Replacements, unsigned>
101103
TokenAnalyzer::process(bool SkipAnnotation) {
104+
LangOpts = getFormattingLangOpts(Style);
105+
102106
tooling::Replacements Result;
103107
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
104-
IdentifierTable IdentTable(getFormattingLangOpts(Style));
108+
IdentifierTable IdentTable(LangOpts);
105109
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
106110
Env.getFirstStartColumn(), Style, Encoding, Allocator,
107111
IdentTable);

clang/lib/Format/TokenAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
namespace clang {
3535
namespace format {
3636

37+
extern LangOptions LangOpts;
38+
3739
class Environment {
3840
public:
3941
// This sets up an virtual file system with file \p FileName containing the

clang/unittests/Format/TestLexer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class TestLexer : public UnwrappedLineConsumer {
6161
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
6262
FormatStyle Style = getLLVMStyle())
6363
: Allocator(Allocator), Buffers(Buffers), Style(Style),
64-
SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
64+
SourceMgr("test.cpp", ""), IdentTable(LangOpts) {
65+
assert(LangOpts.CPlusPlus);
66+
}
6567

6668
TokenList lex(llvm::StringRef Code) {
6769
FormatTokenLexer Lex = getNewLexer(Code);

0 commit comments

Comments
 (0)