Skip to content

Commit 3dc8ef6

Browse files
committed
Revert "[clang-format][NFC] Make LangOpts global in namespace Format (#81390)"
This reverts commit 03f5719. We can't hide getFormattingLangOpts() as it's used by other tools.
1 parent 03f5719 commit 3dc8ef6

File tree

8 files changed

+49
-45
lines changed

8 files changed

+49
-45
lines changed

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_FORMAT_FORMAT_H
1515
#define LLVM_CLANG_FORMAT_FORMAT_H
1616

17+
#include "clang/Basic/LangOptions.h"
1718
#include "clang/Tooling/Core/Replacement.h"
1819
#include "clang/Tooling/Inclusions/IncludeStyle.h"
1920
#include "llvm/ADT/ArrayRef.h"
@@ -5178,6 +5179,11 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
51785179
ArrayRef<tooling::Range> Ranges,
51795180
StringRef FileName = "<stdin>");
51805181

5182+
/// Returns the ``LangOpts`` that the formatter expects you to set.
5183+
///
5184+
/// \param Style determines specific settings for lexing mode.
5185+
LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle());
5186+
51815187
/// Description to be used for help text for a ``llvm::cl`` option for
51825188
/// specifying format style. The description is closely related to the operation
51835189
/// of ``getStyle()``.

clang/lib/Format/Format.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,36 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
38233823
return UsingDeclarationsSorter(*Env, Style).process().first;
38243824
}
38253825

3826+
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
3827+
LangOptions LangOpts;
3828+
3829+
FormatStyle::LanguageStandard LexingStd = Style.Standard;
3830+
if (LexingStd == FormatStyle::LS_Auto)
3831+
LexingStd = FormatStyle::LS_Latest;
3832+
if (LexingStd == FormatStyle::LS_Latest)
3833+
LexingStd = FormatStyle::LS_Cpp20;
3834+
LangOpts.CPlusPlus = 1;
3835+
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
3836+
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
3837+
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
3838+
LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
3839+
LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
3840+
// Turning on digraphs in standards before C++0x is error-prone, because e.g.
3841+
// the sequence "<::" will be unconditionally treated as "[:".
3842+
// Cf. Lexer::LexTokenInternal.
3843+
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
3844+
3845+
LangOpts.LineComment = 1;
3846+
bool AlternativeOperators = Style.isCpp();
3847+
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
3848+
LangOpts.Bool = 1;
3849+
LangOpts.ObjC = 1;
3850+
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
3851+
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
3852+
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
3853+
return LangOpts;
3854+
}
3855+
38263856
const char *StyleOptionHelpDescription =
38273857
"Set coding style. <string> can be:\n"
38283858
"1. A preset: LLVM, GNU, Google, Chromium, Microsoft,\n"

clang/lib/Format/FormatTokenLexer.cpp

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

1515
#include "FormatTokenLexer.h"
16-
#include "TokenAnalyzer.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"
1721

1822
namespace clang {
1923
namespace format {
@@ -24,12 +28,12 @@ FormatTokenLexer::FormatTokenLexer(
2428
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
2529
IdentifierTable &IdentTable)
2630
: FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}),
27-
Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
31+
Column(Column), TrailingWhitespace(0),
32+
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
2833
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
2934
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
3035
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
3136
MacroBlockEndRegex(Style.MacroBlockEnd) {
32-
assert(LangOpts.CPlusPlus);
3337
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
3438
Lex->SetKeepWhitespaceMode(true);
3539

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

14391443
void FormatTokenLexer::resetLexer(unsigned Offset) {
14401444
StringRef Buffer = SourceMgr.getBufferData(ID);
1441-
assert(LangOpts.CPlusPlus);
1445+
LangOpts = getFormattingLangOpts(Style);
14421446
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
14431447
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
14441448
Lex->SetKeepWhitespaceMode(true);

clang/lib/Format/FormatTokenLexer.h

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

1818
#include "Encoding.h"
1919
#include "FormatToken.h"
20+
#include "clang/Basic/LangOptions.h"
2021
#include "clang/Basic/SourceLocation.h"
2122
#include "clang/Basic/SourceManager.h"
2223
#include "clang/Format/Format.h"
@@ -119,6 +120,7 @@ class FormatTokenLexer {
119120
unsigned Column;
120121
unsigned TrailingWhitespace;
121122
std::unique_ptr<Lexer> Lex;
123+
LangOptions LangOpts;
122124
const SourceManager &SourceMgr;
123125
FileID ID;
124126
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-
assert(LangOpts.CPlusPlus);
82+
const auto LangOpts = getFormattingLangOpts(Style);
8383
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
8484
Lex.SetCommentRetentionState(true);
8585

clang/lib/Format/TokenAnalyzer.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,6 @@
3535
namespace clang {
3636
namespace format {
3737

38-
LangOptions LangOpts;
39-
40-
/// Sets `LangOpts` for the formatter.
41-
///
42-
/// \param `Style` determines specific settings for lexing mode.
43-
static void setFormattingLangOpts(const FormatStyle &Style) {
44-
FormatStyle::LanguageStandard LexingStd = Style.Standard;
45-
if (LexingStd == FormatStyle::LS_Auto)
46-
LexingStd = FormatStyle::LS_Latest;
47-
if (LexingStd == FormatStyle::LS_Latest)
48-
LexingStd = FormatStyle::LS_Cpp20;
49-
LangOpts.CPlusPlus = 1;
50-
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
51-
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
52-
LangOpts.CPlusPlus17 = LexingStd >= FormatStyle::LS_Cpp17;
53-
LangOpts.CPlusPlus20 = LexingStd >= FormatStyle::LS_Cpp20;
54-
LangOpts.Char8 = LexingStd >= FormatStyle::LS_Cpp20;
55-
// Turning on digraphs in standards before C++0x is error-prone, because e.g.
56-
// the sequence "<::" will be unconditionally treated as "[:".
57-
// Cf. Lexer::LexTokenInternal.
58-
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
59-
60-
LangOpts.LineComment = 1;
61-
bool AlternativeOperators = Style.isCpp();
62-
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
63-
LangOpts.Bool = 1;
64-
LangOpts.ObjC = 1;
65-
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
66-
LangOpts.DeclSpecKeyword = 1; // To get __declspec.
67-
LangOpts.C99 = 1; // To get kw_restrict for non-underscore-prefixed restrict.
68-
}
69-
7038
// FIXME: Instead of printing the diagnostic we should store it and have a
7139
// better way to return errors through the format APIs.
7240
class FatalDiagnosticConsumer : public DiagnosticConsumer {
@@ -131,11 +99,9 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
13199

132100
std::pair<tooling::Replacements, unsigned>
133101
TokenAnalyzer::process(bool SkipAnnotation) {
134-
setFormattingLangOpts(Style);
135-
136102
tooling::Replacements Result;
137103
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
138-
IdentifierTable IdentTable(LangOpts);
104+
IdentifierTable IdentTable(getFormattingLangOpts(Style));
139105
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
140106
Env.getFirstStartColumn(), Style, Encoding, Allocator,
141107
IdentTable);

clang/lib/Format/TokenAnalyzer.h

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

37-
extern LangOptions LangOpts;
38-
3937
class Environment {
4038
public:
4139
// This sets up an virtual file system with file \p FileName containing the

clang/unittests/Format/TestLexer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ 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(LangOpts) {
65-
assert(LangOpts.CPlusPlus);
66-
}
64+
SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
6765

6866
TokenList lex(llvm::StringRef Code) {
6967
FormatTokenLexer Lex = getNewLexer(Code);

0 commit comments

Comments
 (0)