Skip to content

Commit 03f5719

Browse files
authored
[clang-format][NFC] Make LangOpts global in namespace Format (llvm#81390)
1 parent ffab5a0 commit 03f5719

File tree

8 files changed

+45
-49
lines changed

8 files changed

+45
-49
lines changed

clang/include/clang/Format/Format.h

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

17-
#include "clang/Basic/LangOptions.h"
1817
#include "clang/Tooling/Core/Replacement.h"
1918
#include "clang/Tooling/Inclusions/IncludeStyle.h"
2019
#include "llvm/ADT/ArrayRef.h"
@@ -5179,11 +5178,6 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
51795178
ArrayRef<tooling::Range> Ranges,
51805179
StringRef FileName = "<stdin>");
51815180

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-
51875181
/// Description to be used for help text for a ``llvm::cl`` option for
51885182
/// specifying format style. The description is closely related to the operation
51895183
/// of ``getStyle()``.

clang/lib/Format/Format.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,36 +3823,6 @@ 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-
38563826
const char *StyleOptionHelpDescription =
38573827
"Set coding style. <string> can be:\n"
38583828
"1. A preset: LLVM, GNU, Google, Chromium, Microsoft,\n"

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "Encoding.h"
1919
#include "FormatToken.h"
20-
#include "clang/Basic/LangOptions.h"
2120
#include "clang/Basic/SourceLocation.h"
2221
#include "clang/Basic/SourceManager.h"
2322
#include "clang/Format/Format.h"
@@ -120,7 +119,6 @@ class FormatTokenLexer {
120119
unsigned Column;
121120
unsigned TrailingWhitespace;
122121
std::unique_ptr<Lexer> Lex;
123-
LangOptions LangOpts;
124122
const SourceManager &SourceMgr;
125123
FileID ID;
126124
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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@
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+
3870
// FIXME: Instead of printing the diagnostic we should store it and have a
3971
// better way to return errors through the format APIs.
4072
class FatalDiagnosticConsumer : public DiagnosticConsumer {
@@ -99,9 +131,11 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
99131

100132
std::pair<tooling::Replacements, unsigned>
101133
TokenAnalyzer::process(bool SkipAnnotation) {
134+
setFormattingLangOpts(Style);
135+
102136
tooling::Replacements Result;
103137
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
104-
IdentifierTable IdentTable(getFormattingLangOpts(Style));
138+
IdentifierTable IdentTable(LangOpts);
105139
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
106140
Env.getFirstStartColumn(), Style, Encoding, Allocator,
107141
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)