Skip to content

Commit 0559eaf

Browse files
Revert "Pass LangOpts from CompilerInstance to DependencyScanningWorker (#93753)" (#94488)
This reverts commit 9862080.
1 parent 7dcff59 commit 0559eaf

File tree

8 files changed

+31
-95
lines changed

8 files changed

+31
-95
lines changed

clang/include/clang/Lex/DependencyDirectivesScanner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
1818
#define LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
1919

20-
#include "clang/Basic/LangOptions.h"
2120
#include "clang/Basic/SourceLocation.h"
2221
#include "llvm/ADT/ArrayRef.h"
2322

@@ -118,7 +117,7 @@ struct Directive {
118117
bool scanSourceForDependencyDirectives(
119118
StringRef Input, SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
120119
SmallVectorImpl<dependency_directives_scan::Directive> &Directives,
121-
const LangOptions &LangOpts, DiagnosticsEngine *Diags = nullptr,
120+
DiagnosticsEngine *Diags = nullptr,
122121
SourceLocation InputSourceLoc = SourceLocation());
123122

124123
/// Print the previously scanned dependency directives as minimized source text.

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ class DependencyScanningWorkerFilesystem
363363
///
364364
/// Returns true if the directive tokens are populated for this file entry,
365365
/// false if not (i.e. this entry is not a file or its scan fails).
366-
bool ensureDirectiveTokensArePopulated(EntryRef Entry,
367-
const LangOptions &LangOpts);
366+
bool ensureDirectiveTokensArePopulated(EntryRef Entry);
368367

369368
/// Check whether \p Path exists. By default checks cached result of \c
370369
/// status(), and falls back on FS if unable to do so.

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,8 +1169,8 @@ void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
11691169
llvm::SmallVector<dependency_directives_scan::Token, 16> Tokens;
11701170
llvm::SmallVector<dependency_directives_scan::Directive, 32> Directives;
11711171
if (scanSourceForDependencyDirectives(
1172-
FromFile.getBuffer(), Tokens, Directives, CI.getLangOpts(),
1173-
&CI.getDiagnostics(), SM.getLocForStartOfFile(SM.getMainFileID()))) {
1172+
FromFile.getBuffer(), Tokens, Directives, &CI.getDiagnostics(),
1173+
SM.getLocForStartOfFile(SM.getMainFileID()))) {
11741174
assert(CI.getDiagnostics().hasErrorOccurred() &&
11751175
"no errors reported for failure");
11761176

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,14 @@ struct DirectiveWithTokens {
6262
struct Scanner {
6363
Scanner(StringRef Input,
6464
SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
65-
DiagnosticsEngine *Diags, SourceLocation InputSourceLoc,
66-
const LangOptions &LangOpts)
65+
DiagnosticsEngine *Diags, SourceLocation InputSourceLoc)
6766
: Input(Input), Tokens(Tokens), Diags(Diags),
68-
InputSourceLoc(InputSourceLoc),
69-
LangOpts(getLangOptsForDepScanning(LangOpts)),
70-
TheLexer(InputSourceLoc, this->LangOpts, Input.begin(), Input.begin(),
67+
InputSourceLoc(InputSourceLoc), LangOpts(getLangOptsForDepScanning()),
68+
TheLexer(InputSourceLoc, LangOpts, Input.begin(), Input.begin(),
7169
Input.end()) {}
7270

73-
static LangOptions
74-
getLangOptsForDepScanning(const LangOptions &invocationLangOpts) {
75-
LangOptions LangOpts(invocationLangOpts);
71+
static LangOptions getLangOptsForDepScanning() {
72+
LangOptions LangOpts;
7673
// Set the lexer to use 'tok::at' for '@', instead of 'tok::unknown'.
7774
LangOpts.ObjC = true;
7875
LangOpts.LineComment = true;
@@ -703,7 +700,7 @@ bool Scanner::lex_Pragma(const char *&First, const char *const End) {
703700
SmallVector<dependency_directives_scan::Token> DiscardTokens;
704701
const char *Begin = Buffer.c_str();
705702
Scanner PragmaScanner{StringRef(Begin, Buffer.size()), DiscardTokens, Diags,
706-
InputSourceLoc, LangOptions()};
703+
InputSourceLoc};
707704

708705
PragmaScanner.TheLexer.setParsingPreprocessorDirective(true);
709706
if (PragmaScanner.lexPragma(Begin, Buffer.end()))
@@ -953,10 +950,9 @@ bool Scanner::scan(SmallVectorImpl<Directive> &Directives) {
953950

954951
bool clang::scanSourceForDependencyDirectives(
955952
StringRef Input, SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
956-
SmallVectorImpl<Directive> &Directives, const LangOptions &LangOpts,
957-
DiagnosticsEngine *Diags, SourceLocation InputSourceLoc) {
958-
return Scanner(Input, Tokens, Diags, InputSourceLoc, LangOpts)
959-
.scan(Directives);
953+
SmallVectorImpl<Directive> &Directives, DiagnosticsEngine *Diags,
954+
SourceLocation InputSourceLoc) {
955+
return Scanner(Input, Tokens, Diags, InputSourceLoc).scan(Directives);
960956
}
961957

962958
void clang::printDependencyDirectivesAsSource(

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ DependencyScanningWorkerFilesystem::readFile(StringRef Filename) {
4242
}
4343

4444
bool DependencyScanningWorkerFilesystem::ensureDirectiveTokensArePopulated(
45-
EntryRef Ref, const LangOptions &LangOpts) {
45+
EntryRef Ref) {
4646
auto &Entry = Ref.Entry;
4747

4848
if (Entry.isError() || Entry.isDirectory())
@@ -66,7 +66,7 @@ bool DependencyScanningWorkerFilesystem::ensureDirectiveTokensArePopulated(
6666
// dependencies.
6767
if (scanSourceForDependencyDirectives(Contents->Original->getBuffer(),
6868
Contents->DepDirectiveTokens,
69-
Directives, LangOpts)) {
69+
Directives)) {
7070
Contents->DepDirectiveTokens.clear();
7171
// FIXME: Propagate the diagnostic if desired by the client.
7272
Contents->DepDirectives.store(new std::optional<DependencyDirectivesTy>());

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,11 @@ class DependencyScanningAction : public tooling::ToolAction {
364364
// Use the dependency scanning optimized file system if requested to do so.
365365
if (DepFS)
366366
ScanInstance.getPreprocessorOpts().DependencyDirectivesForFile =
367-
[LocalDepFS = DepFS,
368-
&LangOpts = ScanInstance.getLangOpts()](FileEntryRef File)
367+
[LocalDepFS = DepFS](FileEntryRef File)
369368
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
370369
if (llvm::ErrorOr<EntryRef> Entry =
371370
LocalDepFS->getOrCreateFileSystemEntry(File.getName()))
372-
if (LocalDepFS->ensureDirectiveTokensArePopulated(*Entry, LangOpts))
371+
if (LocalDepFS->ensureDirectiveTokensArePopulated(*Entry))
373372
return Entry->getDirectiveTokens();
374373
return std::nullopt;
375374
};

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Lex/DependencyDirectivesScanner.h"
10-
#include "clang/Basic/TokenKinds.h"
1110
#include "llvm/ADT/SmallString.h"
1211
#include "gtest/gtest.h"
1312

@@ -18,11 +17,11 @@ using namespace clang::dependency_directives_scan;
1817
static bool minimizeSourceToDependencyDirectives(
1918
StringRef Input, SmallVectorImpl<char> &Out,
2019
SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
21-
SmallVectorImpl<Directive> &Directives, const LangOptions &LangOpts) {
20+
SmallVectorImpl<Directive> &Directives) {
2221
Out.clear();
2322
Tokens.clear();
2423
Directives.clear();
25-
if (scanSourceForDependencyDirectives(Input, Tokens, Directives, LangOpts))
24+
if (scanSourceForDependencyDirectives(Input, Tokens, Directives))
2625
return true;
2726

2827
raw_svector_ostream OS(Out);
@@ -39,9 +38,7 @@ static bool minimizeSourceToDependencyDirectives(StringRef Input,
3938
SmallVectorImpl<char> &Out) {
4039
SmallVector<dependency_directives_scan::Token, 16> Tokens;
4140
SmallVector<Directive, 32> Directives;
42-
LangOptions LangOpts;
43-
return minimizeSourceToDependencyDirectives(Input, Out, Tokens, Directives,
44-
LangOpts);
41+
return minimizeSourceToDependencyDirectives(Input, Out, Tokens, Directives);
4542
}
4643

4744
namespace {
@@ -50,17 +47,16 @@ TEST(MinimizeSourceToDependencyDirectivesTest, Empty) {
5047
SmallVector<char, 128> Out;
5148
SmallVector<dependency_directives_scan::Token, 4> Tokens;
5249
SmallVector<Directive, 4> Directives;
53-
LangOptions LangOpts;
5450

55-
ASSERT_FALSE(minimizeSourceToDependencyDirectives("", Out, Tokens, Directives,
56-
LangOpts));
51+
ASSERT_FALSE(
52+
minimizeSourceToDependencyDirectives("", Out, Tokens, Directives));
5753
EXPECT_TRUE(Out.empty());
5854
EXPECT_TRUE(Tokens.empty());
5955
ASSERT_EQ(1u, Directives.size());
6056
ASSERT_EQ(pp_eof, Directives.back().Kind);
6157

6258
ASSERT_FALSE(minimizeSourceToDependencyDirectives("abc def\nxyz", Out, Tokens,
63-
Directives, LangOpts));
59+
Directives));
6460
EXPECT_STREQ("<TokBeforeEOF>\n", Out.data());
6561
EXPECT_TRUE(Tokens.empty());
6662
ASSERT_EQ(2u, Directives.size());
@@ -72,7 +68,6 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AllTokens) {
7268
SmallVector<char, 128> Out;
7369
SmallVector<dependency_directives_scan::Token, 4> Tokens;
7470
SmallVector<Directive, 4> Directives;
75-
LangOptions LangOpts;
7671

7772
ASSERT_FALSE(
7873
minimizeSourceToDependencyDirectives("#define A\n"
@@ -97,7 +92,7 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AllTokens) {
9792
"export module m;\n"
9893
"import m;\n"
9994
"#pragma clang system_header\n",
100-
Out, Tokens, Directives, LangOpts));
95+
Out, Tokens, Directives));
10196
EXPECT_EQ(pp_define, Directives[0].Kind);
10297
EXPECT_EQ(pp_undef, Directives[1].Kind);
10398
EXPECT_EQ(pp_endif, Directives[2].Kind);
@@ -150,10 +145,9 @@ TEST(MinimizeSourceToDependencyDirectivesTest, Define) {
150145
SmallVector<char, 128> Out;
151146
SmallVector<dependency_directives_scan::Token, 4> Tokens;
152147
SmallVector<Directive, 4> Directives;
153-
LangOptions LangOpts;
154148

155-
ASSERT_FALSE(minimizeSourceToDependencyDirectives(
156-
"#define MACRO", Out, Tokens, Directives, LangOpts));
149+
ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO", Out,
150+
Tokens, Directives));
157151
EXPECT_STREQ("#define MACRO\n", Out.data());
158152
ASSERT_EQ(4u, Tokens.size());
159153
ASSERT_EQ(2u, Directives.size());
@@ -844,16 +838,15 @@ TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) {
844838
SmallVector<char, 128> Out;
845839
SmallVector<dependency_directives_scan::Token, 4> Tokens;
846840
SmallVector<Directive, 4> Directives;
847-
LangOptions LangOpts;
848841

849842
StringRef Source = R"(// comment
850843
#pragma once
851844
// another comment
852845
#include <test.h>
853846
_Pragma("once")
854847
)";
855-
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens,
856-
Directives, LangOpts));
848+
ASSERT_FALSE(
849+
minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
857850
EXPECT_STREQ("#pragma once\n#include <test.h>\n_Pragma(\"once\")\n",
858851
Out.data());
859852
ASSERT_EQ(Directives.size(), 4u);
@@ -933,7 +926,6 @@ TEST(MinimizeSourceToDependencyDirectivesTest, CxxModules) {
933926
SmallVector<char, 128> Out;
934927
SmallVector<dependency_directives_scan::Token, 4> Tokens;
935928
SmallVector<Directive, 4> Directives;
936-
LangOptions LangOpts;
937929

938930
StringRef Source = R"(
939931
module;
@@ -962,8 +954,8 @@ ort \
962954
import f(->a = 3);
963955
}
964956
)";
965-
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens,
966-
Directives, LangOpts));
957+
ASSERT_FALSE(
958+
minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
967959
EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;"
968960
"exp\\\nort import:l[[rename]];"
969961
"import<<=3;import a b d e d e f e;"
@@ -1020,52 +1012,4 @@ TEST(MinimizeSourceToDependencyDirectivesTest, TokensBeforeEOF) {
10201012
EXPECT_STREQ("#ifndef A\n#define A\n#endif\n<TokBeforeEOF>\n", Out.data());
10211013
}
10221014

1023-
TEST(MinimizeSourceToDependencyDirectivesTest, CPlusPlus14PPNumber) {
1024-
SmallVector<char, 128> Out;
1025-
SmallVector<dependency_directives_scan::Token, 4> Tokens;
1026-
SmallVector<Directive, 4> Directives;
1027-
LangOptions LangOpts;
1028-
1029-
StringRef Source = R"(
1030-
#if 123'124
1031-
#endif
1032-
)";
1033-
1034-
LangOpts.CPlusPlus14 = true;
1035-
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens,
1036-
Directives, LangOpts));
1037-
EXPECT_STREQ("#if 123'124\n#endif\n", Out.data());
1038-
ASSERT_EQ(Directives.size(), 3u);
1039-
EXPECT_EQ(Directives[0].Kind, dependency_directives_scan::pp_if);
1040-
EXPECT_EQ(Directives[1].Kind, dependency_directives_scan::pp_endif);
1041-
EXPECT_EQ(Directives[2].Kind, dependency_directives_scan::pp_eof);
1042-
ASSERT_EQ(Tokens.size(), 7u);
1043-
1044-
ASSERT_TRUE(Tokens[0].is(tok::hash));
1045-
ASSERT_TRUE(Tokens[1].is(tok::raw_identifier)); // "if"
1046-
ASSERT_TRUE(Tokens[2].is(tok::numeric_constant)); // 123'124
1047-
ASSERT_TRUE(Tokens[3].is(tok::eod));
1048-
ASSERT_TRUE(Tokens[4].is(tok::hash));
1049-
ASSERT_TRUE(Tokens[5].is(tok::raw_identifier)); // #endif
1050-
ASSERT_TRUE(Tokens[6].is(tok::eod));
1051-
1052-
LangOpts.CPlusPlus14 = false;
1053-
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens,
1054-
Directives, LangOpts));
1055-
EXPECT_STREQ("#if 123'124\n#endif\n", Out.data());
1056-
ASSERT_EQ(Directives.size(), 3u);
1057-
EXPECT_EQ(Directives[0].Kind, dependency_directives_scan::pp_if);
1058-
EXPECT_EQ(Directives[1].Kind, dependency_directives_scan::pp_endif);
1059-
EXPECT_EQ(Directives[2].Kind, dependency_directives_scan::pp_eof);
1060-
ASSERT_EQ(Tokens.size(), 8u);
1061-
ASSERT_TRUE(Tokens[0].is(tok::hash));
1062-
ASSERT_TRUE(Tokens[1].is(tok::raw_identifier)); // "if"
1063-
ASSERT_TRUE(Tokens[2].is(tok::numeric_constant)); // 123
1064-
ASSERT_TRUE(Tokens[3].is(tok::unknown)); // '124
1065-
ASSERT_TRUE(Tokens[4].is(tok::eod));
1066-
ASSERT_TRUE(Tokens[5].is(tok::hash));
1067-
ASSERT_TRUE(Tokens[6].is(tok::raw_identifier)); // #endif
1068-
ASSERT_TRUE(Tokens[7].is(tok::eod));
1069-
}
1070-
10711015
} // end anonymous namespace

clang/unittests/Lex/PPDependencyDirectivesTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ TEST_F(PPDependencyDirectivesTest, MacroGuard) {
104104
SmallVector<dependency_directives_scan::Directive> Directives;
105105
};
106106
SmallVector<std::unique_ptr<DepDirectives>> DepDirectivesObjects;
107-
LangOptions LangOpts;
108107

109108
auto getDependencyDirectives = [&](FileEntryRef File)
110109
-> std::optional<ArrayRef<dependency_directives_scan::Directive>> {
111110
DepDirectivesObjects.push_back(std::make_unique<DepDirectives>());
112111
StringRef Input = (*FileMgr.getBufferForFile(File))->getBuffer();
113112
bool Err = scanSourceForDependencyDirectives(
114113
Input, DepDirectivesObjects.back()->Tokens,
115-
DepDirectivesObjects.back()->Directives, LangOpts);
114+
DepDirectivesObjects.back()->Directives);
116115
EXPECT_FALSE(Err);
117116
return llvm::ArrayRef(DepDirectivesObjects.back()->Directives);
118117
};

0 commit comments

Comments
 (0)