Skip to content

Commit 721b68c

Browse files
authored
merge main into amd-staging (llvm#1976)
2 parents 91aa0b6 + 93f4e39 commit 721b68c

File tree

67 files changed

+1451
-2177
lines changed

Some content is hidden

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

67 files changed

+1451
-2177
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ SlabTuple indexSymbols(ASTContext &AST, Preprocessor &PP,
4848
const MainFileMacros *MacroRefsToIndex,
4949
const include_cleaner::PragmaIncludes &PI,
5050
bool IsIndexMainAST, llvm::StringRef Version,
51-
bool CollectMainFileRefs) {
51+
bool CollectMainFileRefs, SymbolOrigin Origin) {
5252
SymbolCollector::Options CollectorOpts;
5353
CollectorOpts.CollectIncludePath = true;
5454
CollectorOpts.PragmaIncludes = Π
5555
CollectorOpts.CountReferences = false;
56-
CollectorOpts.Origin =
57-
IsIndexMainAST ? SymbolOrigin::Open : SymbolOrigin::Preamble;
56+
CollectorOpts.Origin = Origin;
5857
CollectorOpts.CollectMainFileRefs = CollectMainFileRefs;
5958
// We want stdlib implementation details in the index only if we've opened the
6059
// file in question. This does means xrefs won't work, though.
@@ -221,22 +220,24 @@ FileShardedIndex::getShard(llvm::StringRef Uri) const {
221220
}
222221

223222
SlabTuple indexMainDecls(ParsedAST &AST) {
224-
return indexSymbols(
225-
AST.getASTContext(), AST.getPreprocessor(), AST.getLocalTopLevelDecls(),
226-
&AST.getMacros(), AST.getPragmaIncludes(),
227-
/*IsIndexMainAST=*/true, AST.version(), /*CollectMainFileRefs=*/true);
223+
return indexSymbols(AST.getASTContext(), AST.getPreprocessor(),
224+
AST.getLocalTopLevelDecls(), &AST.getMacros(),
225+
AST.getPragmaIncludes(),
226+
/*IsIndexMainAST=*/true, AST.version(),
227+
/*CollectMainFileRefs=*/true, SymbolOrigin::Open);
228228
}
229229

230230
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST,
231231
Preprocessor &PP,
232-
const include_cleaner::PragmaIncludes &PI) {
232+
const include_cleaner::PragmaIncludes &PI,
233+
SymbolOrigin Origin) {
233234
std::vector<Decl *> DeclsToIndex(
234235
AST.getTranslationUnitDecl()->decls().begin(),
235236
AST.getTranslationUnitDecl()->decls().end());
236237
return indexSymbols(AST, PP, DeclsToIndex,
237238
/*MainFileMacros=*/nullptr, PI,
238239
/*IsIndexMainAST=*/false, Version,
239-
/*CollectMainFileRefs=*/false);
240+
/*CollectMainFileRefs=*/false, Origin);
240241
}
241242

242243
FileSymbols::FileSymbols(IndexContents IdxContents, bool SupportContainedRefs)
@@ -462,7 +463,7 @@ void FileIndex::updatePreamble(PathRef Path, llvm::StringRef Version,
462463
const include_cleaner::PragmaIncludes &PI) {
463464
IndexFileIn IF;
464465
std::tie(IF.Symbols, std::ignore, IF.Relations) =
465-
indexHeaderSymbols(Version, AST, PP, PI);
466+
indexHeaderSymbols(Version, AST, PP, PI, SymbolOrigin::Preamble);
466467
updatePreamble(std::move(IF));
467468
}
468469

clang-tools-extra/clangd/index/FileIndex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ SlabTuple indexMainDecls(ParsedAST &AST);
164164
/// included headers.
165165
SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST,
166166
Preprocessor &PP,
167-
const include_cleaner::PragmaIncludes &PI);
167+
const include_cleaner::PragmaIncludes &PI,
168+
SymbolOrigin Origin);
168169

169170
/// Takes slabs coming from a TU (multiple files) and shards them per
170171
/// declaration location.

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
#include "Compiler.h"
1616
#include "Config.h"
1717
#include "SymbolCollector.h"
18+
#include "clang-include-cleaner/Record.h"
19+
#include "index/FileIndex.h"
1820
#include "index/IndexAction.h"
1921
#include "support/Logger.h"
2022
#include "support/ThreadsafeFS.h"
2123
#include "support/Trace.h"
2224
#include "clang/Basic/LangOptions.h"
2325
#include "clang/Frontend/CompilerInvocation.h"
26+
#include "clang/Frontend/FrontendActions.h"
2427
#include "clang/Lex/PreprocessorOptions.h"
2528
#include "clang/Tooling/Inclusions/StandardLibrary.h"
2629
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -223,33 +226,29 @@ SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
223226
return Symbols;
224227
}
225228

226-
SymbolCollector::Options IndexOpts;
227-
IndexOpts.Origin = SymbolOrigin::StdLib;
228-
IndexOpts.CollectMainFileSymbols = false;
229-
IndexOpts.CollectMainFileRefs = false;
230-
IndexOpts.CollectMacro = true;
231-
IndexOpts.StoreAllDocumentation = true;
232-
// Sadly we can't use IndexOpts.FileFilter to restrict indexing scope.
233-
// Files from outside the StdLibLocation may define true std symbols anyway.
234-
// We end up "blessing" such headers, and can only do that by indexing
235-
// everything first.
236-
237-
// Refs, relations, include graph in the stdlib mostly aren't useful.
238-
auto Action = createStaticIndexingAction(
239-
IndexOpts, [&](SymbolSlab S) { Symbols = std::move(S); }, nullptr,
240-
nullptr, nullptr);
241-
242-
if (!Action->BeginSourceFile(*Clang, Input)) {
229+
SyntaxOnlyAction Action;
230+
231+
if (!Action.BeginSourceFile(*Clang, Input)) {
243232
elog("Standard Library Index: BeginSourceFile() failed");
244233
return Symbols;
245234
}
246235

247-
if (llvm::Error Err = Action->Execute()) {
236+
if (llvm::Error Err = Action.Execute()) {
248237
elog("Standard Library Index: Execute failed: {0}", std::move(Err));
249238
return Symbols;
250239
}
251240

252-
Action->EndSourceFile();
241+
// We don't care about include graph for stdlib headers, so provide a no-op
242+
// PI.
243+
include_cleaner::PragmaIncludes PI;
244+
auto Slabs =
245+
indexHeaderSymbols("", Clang->getASTContext(), Clang->getPreprocessor(),
246+
PI, SymbolOrigin::StdLib);
247+
Symbols = std::move(std::get<0>(Slabs));
248+
249+
// Run EndSourceFile() after indexing completes, so ensure the AST and
250+
// preprocessor state is alive during indexing.
251+
Action.EndSourceFile();
253252

254253
unsigned SymbolsBeforeFilter = Symbols.size();
255254
Symbols = filter(std::move(Symbols), Loc);

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#include "Config.h"
1414
#include "SyncAPI.h"
1515
#include "TestFS.h"
16+
#include "index/Index.h"
1617
#include "index/StdLib.h"
1718
#include "clang/Basic/LangOptions.h"
1819
#include "clang/Basic/SourceManager.h"
20+
#include "llvm/Testing/Support/Error.h"
1921
#include "gmock/gmock.h"
2022
#include "gtest/gtest.h"
2123
#include <memory>
@@ -158,6 +160,52 @@ TEST(StdLibTests, EndToEnd) {
158160
UnorderedElementsAre(StdlibSymbol("list"), StdlibSymbol("vector")));
159161
}
160162

163+
TEST(StdLibTests, StdLibDocComments) {
164+
Config Cfg;
165+
Cfg.Index.StandardLibrary = true;
166+
WithContextValue Enabled(Config::Key, std::move(Cfg));
167+
168+
MockFS FS;
169+
FS.Files["stdlib/vector"] = R"cpp(
170+
namespace std {
171+
struct vector {
172+
/**doc comment*/
173+
struct inner {};
174+
};
175+
}
176+
)cpp";
177+
MockCompilationDatabase CDB;
178+
CDB.ExtraClangFlags.push_back("-isystem" + testPath("stdlib"));
179+
ClangdServer::Options Opts = ClangdServer::optsForTest();
180+
Opts.BuildDynamicSymbolIndex = true; // also used for stdlib index
181+
ClangdServer Server(CDB, FS, Opts);
182+
183+
// Open an empty file. <vector> will not be part of the preamble index,
184+
// but it will be part of the stdlib index.
185+
Server.addDocument(testPath("foo.cc"), "");
186+
187+
// Wait for the stdlib index to be built.
188+
ASSERT_TRUE(Server.blockUntilIdleForTest());
189+
190+
// Check that the index contains the doc comment.
191+
SymbolSlab Result;
192+
EXPECT_THAT_ERROR(runCustomAction(Server, testPath("foo.cc"),
193+
[&](InputsAndAST Inputs) {
194+
FuzzyFindRequest Req;
195+
Req.Query = "inner";
196+
Req.Scopes = {"std::vector::"};
197+
SymbolSlab::Builder Builder;
198+
Inputs.Inputs.Index->fuzzyFind(
199+
Req, [&](const Symbol &Sym) {
200+
Builder.insert(Sym);
201+
});
202+
Result = std::move(Builder).build();
203+
}),
204+
llvm::Succeeded());
205+
ASSERT_EQ(Result.size(), 1u);
206+
EXPECT_EQ(Result.begin()->Documentation, "doc comment");
207+
}
208+
161209
} // namespace
162210
} // namespace clangd
163211
} // namespace clang

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Diagnostics.h"
1313
#include "TestFS.h"
1414
#include "index/FileIndex.h"
15+
#include "index/SymbolOrigin.h"
1516
#include "clang/AST/RecursiveASTVisitor.h"
1617
#include "clang/Basic/Diagnostic.h"
1718
#include "clang/Frontend/CompilerInvocation.h"
@@ -164,7 +165,7 @@ SymbolSlab TestTU::headerSymbols() const {
164165
auto AST = build();
165166
return std::get<0>(indexHeaderSymbols(
166167
/*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
167-
AST.getPragmaIncludes()));
168+
AST.getPragmaIncludes(), SymbolOrigin::Preamble));
168169
}
169170

170171
RefSlab TestTU::headerRefs() const {

clang/include/clang/Basic/BuiltinsRISCVXCV.td

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
class RISCXCVBuiltin<string prototype, string features = ""> : TargetBuiltin {
14+
class RISCVXCVBuiltin<string prototype, string features = ""> : TargetBuiltin {
1515
let Spellings = ["__builtin_riscv_cv_" # NAME];
1616
let Prototype = prototype;
1717
let Features = features;
@@ -21,21 +21,21 @@ let Attributes = [NoThrow, Const] in {
2121
//===----------------------------------------------------------------------===//
2222
// XCValu extension.
2323
//===----------------------------------------------------------------------===//
24-
def alu_slet : RISCXCVBuiltin<"int(int, int)", "xcvalu">;
25-
def alu_sletu : RISCXCVBuiltin<"int(unsigned int, unsigned int)", "xcvalu">;
26-
def alu_exths : RISCXCVBuiltin<"int(int)", "xcvalu">;
27-
def alu_exthz : RISCXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
28-
def alu_extbs : RISCXCVBuiltin<"int(int)", "xcvalu">;
29-
def alu_extbz : RISCXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
24+
def alu_slet : RISCVXCVBuiltin<"int(int, int)", "xcvalu">;
25+
def alu_sletu : RISCVXCVBuiltin<"int(unsigned int, unsigned int)", "xcvalu">;
26+
def alu_exths : RISCVXCVBuiltin<"int(int)", "xcvalu">;
27+
def alu_exthz : RISCVXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
28+
def alu_extbs : RISCVXCVBuiltin<"int(int)", "xcvalu">;
29+
def alu_extbz : RISCVXCVBuiltin<"unsigned int(unsigned int)", "xcvalu">;
3030

31-
def alu_clip : RISCXCVBuiltin<"int(int, int)", "xcvalu">;
32-
def alu_clipu : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int)", "xcvalu">;
33-
def alu_addN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
34-
def alu_adduN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
35-
def alu_addRN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
36-
def alu_adduRN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
37-
def alu_subN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
38-
def alu_subuN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
39-
def alu_subRN : RISCXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
40-
def alu_subuRN : RISCXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
31+
def alu_clip : RISCVXCVBuiltin<"int(int, int)", "xcvalu">;
32+
def alu_clipu : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int)", "xcvalu">;
33+
def alu_addN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
34+
def alu_adduN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
35+
def alu_addRN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
36+
def alu_adduRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
37+
def alu_subN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
38+
def alu_subuN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
39+
def alu_subRN : RISCVXCVBuiltin<"int(int, int, unsigned int)", "xcvalu">;
40+
def alu_subuRN : RISCVXCVBuiltin<"unsigned int(unsigned int, unsigned int, unsigned int)", "xcvalu">;
4141
} // Attributes = [NoThrow, Const]

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class Interpreter {
116116
/// Compiler instance performing the incremental compilation.
117117
std::unique_ptr<CompilerInstance> CI;
118118

119+
/// An optional compiler instance for CUDA offloading
120+
std::unique_ptr<CompilerInstance> DeviceCI;
121+
119122
protected:
120123
// Derived classes can use an extended interface of the Interpreter.
121124
Interpreter(std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,85 @@ bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
134134
return true;
135135
}
136136

137+
template <typename ResultT>
138+
bool Context::evaluateStringRepr(State &Parent, const Expr *SizeExpr,
139+
const Expr *PtrExpr, ResultT &Result) {
140+
assert(Stk.empty());
141+
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
142+
143+
// Evaluate size value.
144+
APValue SizeValue;
145+
if (!evaluateAsRValue(Parent, SizeExpr, SizeValue))
146+
return false;
147+
148+
if (!SizeValue.isInt())
149+
return false;
150+
uint64_t Size = SizeValue.getInt().getZExtValue();
151+
152+
auto PtrRes = C.interpretAsPointer(PtrExpr, [&](const Pointer &Ptr) {
153+
if (Size == 0) {
154+
if constexpr (std::is_same_v<ResultT, APValue>)
155+
Result = APValue(APValue::UninitArray{}, 0, 0);
156+
return true;
157+
}
158+
159+
if (!Ptr.isLive() || !Ptr.getFieldDesc()->isPrimitiveArray())
160+
return false;
161+
162+
// Must be char.
163+
if (Ptr.getFieldDesc()->getElemSize() != 1 /*bytes*/)
164+
return false;
165+
166+
if (Size > Ptr.getNumElems()) {
167+
Parent.FFDiag(SizeExpr, diag::note_constexpr_access_past_end) << AK_Read;
168+
Size = Ptr.getNumElems();
169+
}
170+
171+
if constexpr (std::is_same_v<ResultT, APValue>) {
172+
QualType CharTy = PtrExpr->getType()->getPointeeType();
173+
Result = APValue(APValue::UninitArray{}, Size, Size);
174+
for (uint64_t I = 0; I != Size; ++I) {
175+
if (std::optional<APValue> ElemVal =
176+
Ptr.atIndex(I).toRValue(*this, CharTy))
177+
Result.getArrayInitializedElt(I) = *ElemVal;
178+
else
179+
return false;
180+
}
181+
} else {
182+
assert((std::is_same_v<ResultT, std::string>));
183+
if (Size < Result.max_size())
184+
Result.resize(Size);
185+
Result.assign(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
186+
}
187+
188+
return true;
189+
});
190+
191+
if (PtrRes.isInvalid()) {
192+
C.cleanup();
193+
Stk.clear();
194+
return false;
195+
}
196+
197+
return true;
198+
}
199+
200+
bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
201+
const Expr *PtrExpr, APValue &Result) {
202+
assert(SizeExpr);
203+
assert(PtrExpr);
204+
205+
return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
206+
}
207+
208+
bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
209+
const Expr *PtrExpr, std::string &Result) {
210+
assert(SizeExpr);
211+
assert(PtrExpr);
212+
213+
return evaluateStringRepr(Parent, SizeExpr, PtrExpr, Result);
214+
}
215+
137216
const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
138217

139218
std::optional<PrimType> Context::classify(QualType T) const {

clang/lib/AST/ByteCode/Context.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Context final {
5959
/// Evaluates a toplevel initializer.
6060
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
6161

62+
bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
63+
const Expr *PtrExpr, APValue &Result);
64+
bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
65+
const Expr *PtrExpr, std::string &Result);
66+
6267
/// Returns the AST context.
6368
ASTContext &getASTContext() const { return Ctx; }
6469
/// Returns the language options.
@@ -120,6 +125,10 @@ class Context final {
120125
/// Runs a function.
121126
bool Run(State &Parent, const Function *Func);
122127

128+
template <typename ResultT>
129+
bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
130+
const Expr *PtrExpr, ResultT &Result);
131+
123132
/// Current compilation context.
124133
ASTContext &Ctx;
125134
/// Interpreter stack, shared across invocations.

0 commit comments

Comments
 (0)