Skip to content

Commit 293bc12

Browse files
authored
Merge branch 'main' into stmt-attributes
2 parents 0411b29 + 5df7d88 commit 293bc12

File tree

455 files changed

+20089
-18165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

455 files changed

+20089
-18165
lines changed

.github/workflows/release-binaries-all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ on:
4343
- '.github/workflows/release-binaries.yml'
4444
- '.github/workflows/release-binaries-setup-stage/*'
4545
- '.github/workflows/release-binaries-save-stage/*'
46+
- 'clang/cmake/caches/Release.cmake'
4647

4748
concurrency:
4849
group: ${{ github.workflow }}-${{ github.event.pull_request.number || 'dispatch' }}

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,9 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
14091409
Clang->getPreprocessorOpts().SingleFileParseMode = CompletingInPreamble;
14101410
Clang->setCodeCompletionConsumer(Consumer.release());
14111411

1412+
if (Input.Preamble.RequiredModules)
1413+
Input.Preamble.RequiredModules->adjustHeaderSearchOptions(Clang->getHeaderSearchOpts());
1414+
14121415
SyntaxOnlyAction Action;
14131416
if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
14141417
log("BeginSourceFile() failed when running codeComplete for {0}",
@@ -2122,7 +2125,7 @@ clang::CodeCompleteOptions CodeCompleteOptions::getClangCompleteOpts() const {
21222125
// When an is used, Sema is responsible for completing the main file,
21232126
// the index can provide results from the preamble.
21242127
// Tell Sema not to deserialize the preamble to look for results.
2125-
Result.LoadExternal = !Index;
2128+
Result.LoadExternal = ForceLoadPreamble || !Index;
21262129
Result.IncludeFixIts = IncludeFixIts;
21272130

21282131
return Result;

clang-tools-extra/clangd/CodeComplete.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ struct CodeCompleteOptions {
5252
/// For example, private members are usually inaccessible.
5353
bool IncludeIneligibleResults = false;
5454

55+
/// Force sema to load decls from preamble even if an index is provided.
56+
/// This is helpful for cases the index can't provide symbols, e.g. with
57+
/// experimental c++20 modules
58+
bool ForceLoadPreamble = false;
59+
5560
/// Combine overloads into a single completion item where possible.
5661
/// If none, the implementation may choose an appropriate behavior.
5762
/// (In practice, ClangdLSPServer enables bundling if the client claims

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/ADT/DenseMap.h"
4242
#include "llvm/ADT/SmallVector.h"
4343
#include "llvm/ADT/StringRef.h"
44+
#include "llvm/Support/Casting.h"
4445
#include "llvm/Support/ErrorHandling.h"
4546
#include "llvm/Support/FileSystem.h"
4647
#include "llvm/Support/Path.h"
@@ -75,18 +76,62 @@ bool isPrivateProtoDecl(const NamedDecl &ND) {
7576
if (ND.getIdentifier() == nullptr)
7677
return false;
7778
auto Name = ND.getIdentifier()->getName();
78-
if (!Name.contains('_'))
79-
return false;
80-
// Nested proto entities (e.g. Message::Nested) have top-level decls
81-
// that shouldn't be used (Message_Nested). Ignore them completely.
82-
// The nested entities are dangling type aliases, we may want to reconsider
83-
// including them in the future.
84-
// For enum constants, SOME_ENUM_CONSTANT is not private and should be
85-
// indexed. Outer_INNER is private. This heuristic relies on naming style, it
86-
// will include OUTER_INNER and exclude some_enum_constant.
87-
// FIXME: the heuristic relies on naming style (i.e. no underscore in
88-
// user-defined names) and can be improved.
89-
return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower);
79+
// There are some internal helpers like _internal_set_foo();
80+
if (Name.contains("_internal_"))
81+
return true;
82+
83+
// https://protobuf.dev/reference/cpp/cpp-generated/#nested-types
84+
// Nested entities (messages/enums) has two names, one at the top-level scope,
85+
// with a mangled name created by prepending all the outer types. These names
86+
// are almost never preferred by the developers, so exclude them from index.
87+
// e.g.
88+
// message Foo {
89+
// message Bar {}
90+
// enum E { A }
91+
// }
92+
//
93+
// yields:
94+
// class Foo_Bar {};
95+
// enum Foo_E { Foo_E_A };
96+
// class Foo {
97+
// using Bar = Foo_Bar;
98+
// static constexpr Foo_E A = Foo_E_A;
99+
// };
100+
101+
// We get rid of Foo_Bar and Foo_E by discarding any top-level entries with
102+
// `_` in the name. This relies on original message/enum not having `_` in the
103+
// name. Hence might go wrong in certain cases.
104+
if (ND.getDeclContext()->isNamespace()) {
105+
// Strip off some known public suffix helpers for enums, rest of the helpers
106+
// are generated inside record decls so we don't care.
107+
// https://protobuf.dev/reference/cpp/cpp-generated/#enum
108+
Name.consume_back("_descriptor");
109+
Name.consume_back("_IsValid");
110+
Name.consume_back("_Name");
111+
Name.consume_back("_Parse");
112+
Name.consume_back("_MIN");
113+
Name.consume_back("_MAX");
114+
Name.consume_back("_ARRAYSIZE");
115+
return Name.contains('_');
116+
}
117+
118+
// EnumConstantDecls need some special attention, despite being nested in a
119+
// TagDecl, they might still have mangled names. We filter those by checking
120+
// if it has parent's name as a prefix.
121+
// This might go wrong if a nested entity has a name that starts with parent's
122+
// name, e.g: enum Foo { Foo_X }.
123+
if (llvm::isa<EnumConstantDecl>(&ND)) {
124+
auto *DC = llvm::cast<EnumDecl>(ND.getDeclContext());
125+
if (!DC || !DC->getIdentifier())
126+
return false;
127+
auto CtxName = DC->getIdentifier()->getName();
128+
return !CtxName.empty() && Name.consume_front(CtxName) &&
129+
Name.consume_front("_");
130+
}
131+
132+
// Now we're only left with fields/methods without an `_internal_` in the
133+
// name, they're intended for public use.
134+
return false;
90135
}
91136

92137
// We only collect #include paths for symbols that are suitable for global code

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,9 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
919919
Opts.CodeComplete.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
920920
Opts.CodeComplete.RunParser = CodeCompletionParse;
921921
Opts.CodeComplete.RankingModel = RankingModel;
922+
// FIXME: If we're using C++20 modules, force the lookup process to load
923+
// external decls, since currently the index doesn't support C++20 modules.
924+
Opts.CodeComplete.ForceLoadPreamble = ExperimentalModulesSupport;
922925

923926
RealThreadsafeFS TFS;
924927
std::vector<std::unique_ptr<config::Provider>> ProviderStack;

clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,86 @@ import A;
402402
EXPECT_TRUE(D.isFromASTFile());
403403
}
404404

405+
// An end to end test for code complete in modules
406+
TEST_F(PrerequisiteModulesTests, CodeCompleteTest) {
407+
MockDirectoryCompilationDatabase CDB(TestDir, FS);
408+
409+
CDB.addFile("A.cppm", R"cpp(
410+
export module A;
411+
export void printA();
412+
)cpp");
413+
414+
llvm::StringLiteral UserContents = R"cpp(
415+
import A;
416+
void func() {
417+
print^
418+
}
419+
)cpp";
420+
421+
CDB.addFile("Use.cpp", UserContents);
422+
Annotations Test(UserContents);
423+
424+
ModulesBuilder Builder(CDB);
425+
426+
ParseInputs Use = getInputs("Use.cpp", CDB);
427+
Use.ModulesManager = &Builder;
428+
429+
std::unique_ptr<CompilerInvocation> CI =
430+
buildCompilerInvocation(Use, DiagConsumer);
431+
EXPECT_TRUE(CI);
432+
433+
auto Preamble =
434+
buildPreamble(getFullPath("Use.cpp"), *CI, Use, /*InMemory=*/true,
435+
/*Callback=*/nullptr);
436+
EXPECT_TRUE(Preamble);
437+
EXPECT_TRUE(Preamble->RequiredModules);
438+
439+
auto Result = codeComplete(getFullPath("Use.cpp"), Test.point(),
440+
Preamble.get(), Use, {});
441+
EXPECT_FALSE(Result.Completions.empty());
442+
EXPECT_EQ(Result.Completions[0].Name, "printA");
443+
}
444+
445+
TEST_F(PrerequisiteModulesTests, SignatureHelpTest) {
446+
MockDirectoryCompilationDatabase CDB(TestDir, FS);
447+
448+
CDB.addFile("A.cppm", R"cpp(
449+
export module A;
450+
export void printA(int a);
451+
)cpp");
452+
453+
llvm::StringLiteral UserContents = R"cpp(
454+
import A;
455+
void func() {
456+
printA(^);
457+
}
458+
)cpp";
459+
460+
CDB.addFile("Use.cpp", UserContents);
461+
Annotations Test(UserContents);
462+
463+
ModulesBuilder Builder(CDB);
464+
465+
ParseInputs Use = getInputs("Use.cpp", CDB);
466+
Use.ModulesManager = &Builder;
467+
468+
std::unique_ptr<CompilerInvocation> CI =
469+
buildCompilerInvocation(Use, DiagConsumer);
470+
EXPECT_TRUE(CI);
471+
472+
auto Preamble =
473+
buildPreamble(getFullPath("Use.cpp"), *CI, Use, /*InMemory=*/true,
474+
/*Callback=*/nullptr);
475+
EXPECT_TRUE(Preamble);
476+
EXPECT_TRUE(Preamble->RequiredModules);
477+
478+
auto Result = signatureHelp(getFullPath("Use.cpp"), Test.point(),
479+
*Preamble.get(), Use, MarkupKind::PlainText);
480+
EXPECT_FALSE(Result.signatures.empty());
481+
EXPECT_EQ(Result.signatures[0].label, "printA(int a) -> void");
482+
EXPECT_EQ(Result.signatures[0].parameters[0].labelString, "int a");
483+
}
484+
405485
} // namespace
406486
} // namespace clang::clangd
407487

clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,63 @@ TEST_F(ShouldCollectSymbolTest, NoPrivateProtoSymbol) {
201201
build(
202202
R"(// Generated by the protocol buffer compiler. DO NOT EDIT!
203203
namespace nx {
204-
class Top_Level {};
205-
class TopLevel {};
206-
enum Kind {
207-
KIND_OK,
208-
Kind_Not_Ok,
204+
enum Outer_Enum : int {
205+
Outer_Enum_KIND1,
206+
Outer_Enum_Kind_2,
209207
};
208+
bool Outer_Enum_IsValid(int);
209+
210+
class Outer_Inner {};
211+
class Outer {
212+
using Inner = Outer_Inner;
213+
using Enum = Outer_Enum;
214+
static constexpr Enum KIND1 = Outer_Enum_KIND1;
215+
static constexpr Enum Kind_2 = Outer_Enum_Kind_2;
216+
static bool Enum_IsValid(int);
217+
int &x();
218+
void set_x();
219+
void _internal_set_x();
220+
221+
int &Outer_y();
222+
};
223+
enum Foo {
224+
FOO_VAL1,
225+
Foo_VAL2,
226+
};
227+
bool Foo_IsValid(int);
210228
})");
211-
EXPECT_TRUE(shouldCollect("nx::TopLevel"));
212-
EXPECT_TRUE(shouldCollect("nx::Kind::KIND_OK"));
213-
EXPECT_TRUE(shouldCollect("nx::Kind"));
214229

215-
EXPECT_FALSE(shouldCollect("nx::Top_Level"));
216-
EXPECT_FALSE(shouldCollect("nx::Kind::Kind_Not_Ok"));
230+
// Make sure all the mangled names for Outer::Enum is discarded.
231+
EXPECT_FALSE(shouldCollect("nx::Outer_Enum"));
232+
EXPECT_FALSE(shouldCollect("nx::Outer_Enum_KIND1"));
233+
EXPECT_FALSE(shouldCollect("nx::Outer_Enum_Kind_2"));
234+
EXPECT_FALSE(shouldCollect("nx::Outer_Enum_IsValid"));
235+
// But nested aliases are preserved.
236+
EXPECT_TRUE(shouldCollect("nx::Outer::Enum"));
237+
EXPECT_TRUE(shouldCollect("nx::Outer::KIND1"));
238+
EXPECT_TRUE(shouldCollect("nx::Outer::Kind_2"));
239+
EXPECT_TRUE(shouldCollect("nx::Outer::Enum_IsValid"));
240+
241+
// Check for Outer::Inner.
242+
EXPECT_FALSE(shouldCollect("nx::Outer_Inner"));
243+
EXPECT_TRUE(shouldCollect("nx::Outer"));
244+
EXPECT_TRUE(shouldCollect("nx::Outer::Inner"));
245+
246+
// Make sure field related information is preserved, unless it's explicitly
247+
// marked with `_internal_`.
248+
EXPECT_TRUE(shouldCollect("nx::Outer::x"));
249+
EXPECT_TRUE(shouldCollect("nx::Outer::set_x"));
250+
EXPECT_FALSE(shouldCollect("nx::Outer::_internal_set_x"));
251+
EXPECT_TRUE(shouldCollect("nx::Outer::Outer_y"));
252+
253+
// Handling of a top-level enum
254+
EXPECT_TRUE(shouldCollect("nx::Foo::FOO_VAL1"));
255+
EXPECT_TRUE(shouldCollect("nx::FOO_VAL1"));
256+
EXPECT_TRUE(shouldCollect("nx::Foo_IsValid"));
257+
// Our heuristic goes wrong here, if the user has a nested name that starts
258+
// with parent's name.
259+
EXPECT_FALSE(shouldCollect("nx::Foo::Foo_VAL2"));
260+
EXPECT_FALSE(shouldCollect("nx::Foo_VAL2"));
217261
}
218262

219263
TEST_F(ShouldCollectSymbolTest, DoubleCheckProtoHeaderComment) {

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/SmallVector.h"
2222
#include "llvm/ADT/StringRef.h"
2323
#include <string>
24+
#include <utility>
2425

2526
namespace clang {
2627
class SourceLocation;
@@ -62,7 +63,8 @@ void walkUsed(llvm::ArrayRef<Decl *> ASTRoots,
6263

6364
struct AnalysisResults {
6465
std::vector<const Include *> Unused;
65-
std::vector<std::string> Missing; // Spellings, like "<vector>"
66+
// Spellings, like "<vector>" paired with the Header that generated it.
67+
std::vector<std::pair<std::string, Header>> Missing;
6668
};
6769

6870
/// Determine which headers should be inserted or removed from the main file.

clang-tools-extra/include-cleaner/lib/Analysis.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include "llvm/ADT/STLExtras.h"
2727
#include "llvm/ADT/STLFunctionalExtras.h"
2828
#include "llvm/ADT/SmallVector.h"
29+
#include "llvm/ADT/StringMap.h"
2930
#include "llvm/ADT/StringRef.h"
30-
#include "llvm/ADT/StringSet.h"
3131
#include "llvm/Support/Error.h"
3232
#include "llvm/Support/ErrorHandling.h"
3333
#include <cassert>
@@ -84,7 +84,7 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
8484
auto &SM = PP.getSourceManager();
8585
const auto MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
8686
llvm::DenseSet<const Include *> Used;
87-
llvm::StringSet<> Missing;
87+
llvm::StringMap<Header> Missing;
8888
if (!HeaderFilter)
8989
HeaderFilter = [](llvm::StringRef) { return false; };
9090
OptionalDirectoryEntryRef ResourceDir =
@@ -119,7 +119,7 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
119119
Satisfied = true;
120120
}
121121
if (!Satisfied)
122-
Missing.insert(std::move(Spelling));
122+
Missing.try_emplace(std::move(Spelling), Providers.front());
123123
});
124124

125125
AnalysisResults Results;
@@ -144,8 +144,8 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
144144
}
145145
Results.Unused.push_back(&I);
146146
}
147-
for (llvm::StringRef S : Missing.keys())
148-
Results.Missing.push_back(S.str());
147+
for (auto &E : Missing)
148+
Results.Missing.emplace_back(E.first().str(), E.second);
149149
llvm::sort(Results.Missing);
150150
return Results;
151151
}
@@ -158,9 +158,9 @@ std::string fixIncludes(const AnalysisResults &Results,
158158
// Encode insertions/deletions in the magic way clang-format understands.
159159
for (const Include *I : Results.Unused)
160160
cantFail(R.add(tooling::Replacement(FileName, UINT_MAX, 1, I->quote())));
161-
for (llvm::StringRef Spelled : Results.Missing)
162-
cantFail(R.add(tooling::Replacement(FileName, UINT_MAX, 0,
163-
("#include " + Spelled).str())));
161+
for (auto &[Spelled, _] : Results.Missing)
162+
cantFail(R.add(
163+
tooling::Replacement(FileName, UINT_MAX, 0, "#include " + Spelled)));
164164
// "cleanup" actually turns the UINT_MAX replacements into concrete edits.
165165
auto Positioned = cantFail(format::cleanupAroundReplacements(Code, R, Style));
166166
return cantFail(tooling::applyAllReplacements(Code, Positioned));

clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Action : public clang::ASTFrontendAction {
192192
case PrintStyle::Changes:
193193
for (const Include *I : Results.Unused)
194194
llvm::outs() << "- " << I->quote() << " @Line:" << I->Line << "\n";
195-
for (const auto &I : Results.Missing)
195+
for (const auto &[I, _] : Results.Missing)
196196
llvm::outs() << "+ " << I << "\n";
197197
break;
198198
case PrintStyle::Final:

0 commit comments

Comments
 (0)