Skip to content

Commit c9f1900

Browse files
authored
Merge pull request #27464 from nkcsgexi/deserialize-source-info
SerializeLoc: serialize basic decl source location information to .swiftsourceinfo file
2 parents 54ec600 + 483bd5d commit c9f1900

Some content is hidden

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

42 files changed

+1093
-171
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,12 @@ function(_compile_swift_files
310310
set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")
311311
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
312312
set(specific_module_dir "${module_base}.swiftmodule")
313-
set(specific_module_private_dir "${specific_module_dir}/Private")
314-
set(source_info_file "${specific_module_private_dir}/${SWIFTFILE_ARCHITECTURE}.swiftsourceinfo")
313+
set(specific_module_project_dir "${specific_module_dir}/Project")
314+
set(source_info_file "${specific_module_project_dir}/${SWIFTFILE_ARCHITECTURE}.swiftsourceinfo")
315315
set(module_base "${module_base}.swiftmodule/${SWIFTFILE_ARCHITECTURE}")
316316
else()
317317
set(specific_module_dir)
318-
set(specific_module_private_dir)
318+
set(specific_module_project_dir)
319319
set(source_info_file "${module_base}.swiftsourceinfo")
320320
endif()
321321
set(module_file "${module_base}.swiftmodule")
@@ -359,7 +359,7 @@ function(_compile_swift_files
359359
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
360360
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
361361
OPTIONAL
362-
PATTERN "Private" EXCLUDE)
362+
PATTERN "Project" EXCLUDE)
363363
else()
364364
swift_install_in_component(FILES ${module_outputs}
365365
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
@@ -500,7 +500,7 @@ function(_compile_swift_files
500500
COMMAND
501501
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
502502
${specific_module_dir}
503-
${specific_module_private_dir}
503+
${specific_module_project_dir}
504504
COMMAND
505505
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
506506
"${swift_compiler_tool}" "-emit-module" "-o" "${module_file}"

include/swift/AST/FileUnit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class FileUnit : public DeclContext {
131131
return None;
132132
}
133133

134+
virtual Optional<BasicDeclLocs> getBasicLocsForDecl(const Decl *D) const {
135+
return None;
136+
}
137+
134138
virtual void collectAllGroups(std::vector<StringRef> &Names) const {}
135139

136140
/// Returns an implementation-defined "discriminator" for \p D, which

include/swift/AST/RawComment.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ struct CommentInfo {
7878
uint32_t SourceOrder;
7979
};
8080

81+
struct LineColumn {
82+
uint32_t Line = 0;
83+
uint32_t Column = 0;
84+
bool isValid() const { return Line && Column; }
85+
};
86+
87+
struct BasicDeclLocs {
88+
StringRef SourceFilePath;
89+
LineColumn Loc;
90+
LineColumn StartLoc;
91+
LineColumn EndLoc;
92+
};
93+
8194
} // namespace swift
8295

8396
#endif // LLVM_SWIFT_AST_RAW_COMMENT_H

include/swift/AST/SourceFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class SourceFile final : public FileUnit {
276276

277277
Identifier getDiscriminatorForPrivateValue(const ValueDecl *D) const override;
278278
Identifier getPrivateDiscriminator() const { return PrivateDiscriminator; }
279+
Optional<BasicDeclLocs> getBasicLocsForDecl(const Decl *D) const override;
279280

280281
virtual bool walk(ASTWalker &walker) override;
281282

include/swift/AST/USRGeneration.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/LLVM.h"
2323

2424
namespace swift {
25+
class Decl;
2526
class AbstractStorageDecl;
2627
class ValueDecl;
2728
class ExtensionDecl;
@@ -39,9 +40,9 @@ bool printTypeUSR(Type Ty, raw_ostream &OS);
3940
/// \returns true if it failed, false on success.
4041
bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
4142

42-
/// Prints out the USR for the given Decl.
43+
/// Prints out the USR for the given ValueDecl.
4344
/// \returns true if it failed, false on success.
44-
bool printDeclUSR(const ValueDecl *D, raw_ostream &OS);
45+
bool printValueDeclUSR(const ValueDecl *D, raw_ostream &OS);
4546

4647
/// Prints out the USR for the given ModuleEntity.
4748
/// \returns true if it failed, false on success.
@@ -56,6 +57,10 @@ bool printAccessorUSR(const AbstractStorageDecl *D, AccessorKind AccKind,
5657
/// \returns true if it failed, false on success.
5758
bool printExtensionUSR(const ExtensionDecl *ED, raw_ostream &OS);
5859

60+
/// Prints out the USR for the given Decl.
61+
/// \returns true if it failed, false on success.
62+
bool printDeclUSR(const Decl *D, raw_ostream &OS);
63+
5964
} // namespace ide
6065
} // namespace swift
6166

include/swift/Frontend/Frontend.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ class CompilerInstance {
395395
struct PartialModuleInputs {
396396
std::unique_ptr<llvm::MemoryBuffer> ModuleBuffer;
397397
std::unique_ptr<llvm::MemoryBuffer> ModuleDocBuffer;
398+
std::unique_ptr<llvm::MemoryBuffer> ModuleSourceInfoBuffer;
398399
};
399400

400401
/// Contains \c MemoryBuffers for partial serialized module files and
@@ -555,20 +556,35 @@ class CompilerInstance {
555556

556557
Optional<unsigned> getRecordedBufferID(const InputFile &input, bool &failed);
557558

559+
struct ModuleBuffers {
560+
std::unique_ptr<llvm::MemoryBuffer> ModuleBuffer;
561+
std::unique_ptr<llvm::MemoryBuffer> ModuleDocBuffer;
562+
std::unique_ptr<llvm::MemoryBuffer> ModuleSourceInfoBuffer;
563+
ModuleBuffers(std::unique_ptr<llvm::MemoryBuffer> ModuleBuffer,
564+
std::unique_ptr<llvm::MemoryBuffer> ModuleDocBuffer = nullptr,
565+
std::unique_ptr<llvm::MemoryBuffer> ModuleSourceInfoBuffer = nullptr):
566+
ModuleBuffer(std::move(ModuleBuffer)),
567+
ModuleDocBuffer(std::move(ModuleDocBuffer)),
568+
ModuleSourceInfoBuffer(std::move(ModuleSourceInfoBuffer)) {}
569+
};
570+
558571
/// Given an input file, return a buffer to use for its contents,
559572
/// and a buffer for the corresponding module doc file if one exists.
560573
/// On failure, return a null pointer for the first element of the returned
561574
/// pair.
562-
std::pair<std::unique_ptr<llvm::MemoryBuffer>,
563-
std::unique_ptr<llvm::MemoryBuffer>>
564-
getInputBufferAndModuleDocBufferIfPresent(const InputFile &input);
575+
Optional<ModuleBuffers> getInputBuffersIfPresent(const InputFile &input);
565576

566577
/// Try to open the module doc file corresponding to the input parameter.
567578
/// Return None for error, nullptr if no such file exists, or the buffer if
568579
/// one was found.
569580
Optional<std::unique_ptr<llvm::MemoryBuffer>>
570581
openModuleDoc(const InputFile &input);
571582

583+
/// Try to open the module source info file corresponding to the input parameter.
584+
/// Return None for error, nullptr if no such file exists, or the buffer if
585+
/// one was found.
586+
Optional<std::unique_ptr<llvm::MemoryBuffer>>
587+
openModuleSourceInfo(const InputFile &input);
572588
public:
573589
/// Parses and type-checks all input files.
574590
void performSema();

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
149149
std::error_code findModuleFilesInDirectory(
150150
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
151151
StringRef ModuleDocFilename,
152+
StringRef ModuleSourceInfoFilename,
152153
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
153-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
154+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
155+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) override;
154156

155157
bool isCached(StringRef DepPath) override;
156158

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
5353
bool findModule(AccessPathElem moduleID,
5454
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
5555
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
56+
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
5657
bool &isFramework, bool &isSystemModule);
5758

5859
/// Attempts to search the provided directory for a loadable serialized
@@ -70,20 +71,30 @@ class SerializedModuleLoaderBase : public ModuleLoader {
7071
virtual std::error_code findModuleFilesInDirectory(
7172
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
7273
StringRef ModuleDocFilename,
74+
StringRef ModuleSourceInfoFilename,
7375
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
74-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) = 0;
76+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
77+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) = 0;
7578

7679
std::error_code
7780
openModuleFiles(AccessPathElem ModuleID,
7881
StringRef ModulePath, StringRef ModuleDocPath,
82+
StringRef ModuleSourceInfoName,
7983
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
80-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
84+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
85+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer);
8186

8287
std::error_code
8388
openModuleDocFile(AccessPathElem ModuleID,
8489
StringRef ModuleDocPath,
8590
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
8691

92+
void
93+
openModuleSourceInfoFileIfPresent(AccessPathElem ModuleID,
94+
StringRef ModulePath,
95+
StringRef ModuleSourceInfoFileName,
96+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer);
97+
8798
/// If the module loader subclass knows that all options have been tried for
8899
/// loading an architecture-specific file out of a swiftmodule bundle, try
89100
/// to list the architectures that \e are present.
@@ -116,6 +127,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
116127
FileUnit *loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
117128
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
118129
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
130+
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
119131
bool isFramework, bool treatAsPartialModule);
120132

121133
/// Check whether the module with a given name can be imported without
@@ -163,8 +175,10 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
163175
std::error_code findModuleFilesInDirectory(
164176
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
165177
StringRef ModuleDocFilename,
178+
StringRef ModuleSourceInfoFilename,
166179
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
167-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
180+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
181+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) override;
168182

169183
bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
170184
StringRef moduleName,
@@ -203,8 +217,10 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
203217
std::error_code findModuleFilesInDirectory(
204218
AccessPathElem ModuleID, StringRef DirPath, StringRef ModuleFilename,
205219
StringRef ModuleDocFilename,
220+
StringRef ModuleSourceInfoFilename,
206221
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
207-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
222+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
223+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) override;
208224

209225
bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
210226
StringRef moduleName,
@@ -317,6 +333,8 @@ class SerializedASTFile final : public LoadedFile {
317333

318334
Optional<StringRef> getGroupNameByUSR(StringRef USR) const override;
319335

336+
Optional<BasicDeclLocs> getBasicLocsForDecl(const Decl *D) const override;
337+
320338
void collectAllGroups(std::vector<StringRef> &Names) const override;
321339

322340
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ static unsigned getUnnamedParamIndex(const ParamDecl *D) {
587587

588588
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D->getDeclContext())) {
589589
ParamList = AFD->getParameters();
590+
} else if (auto EED = dyn_cast<EnumElementDecl>(D->getDeclContext())) {
591+
ParamList = EED->getParameterList();
590592
} else {
591593
auto ACE = cast<AbstractClosureExpr>(D->getDeclContext());
592594
ParamList = ACE->getParameters();

lib/AST/Module.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,32 @@ TypeDecl *SourceFile::lookupLocalType(llvm::StringRef mangledName) const {
686686
return nullptr;
687687
}
688688

689+
Optional<BasicDeclLocs>
690+
SourceFile::getBasicLocsForDecl(const Decl *D) const {
691+
auto *FileCtx = D->getDeclContext()->getModuleScopeContext();
692+
assert(FileCtx == this && "D doesn't belong to this source file");
693+
if (FileCtx != this) {
694+
// D doesn't belong to this file. This shouldn't happen in practice.
695+
return None;
696+
}
697+
if (D->getLoc().isInvalid())
698+
return None;
699+
SourceManager &SM = getASTContext().SourceMgr;
700+
BasicDeclLocs Result;
701+
Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc());
702+
auto setLineColumn = [&SM](LineColumn &Home, SourceLoc Loc) {
703+
if (Loc.isValid()) {
704+
std::tie(Home.Line, Home.Column) = SM.getLineAndColumn(Loc);
705+
}
706+
};
707+
#define SET(X) setLineColumn(Result.X, D->get##X());
708+
SET(Loc)
709+
SET(StartLoc)
710+
SET(EndLoc)
711+
#undef SET
712+
return Result;
713+
}
714+
689715
void ModuleDecl::getDisplayDecls(SmallVectorImpl<Decl*> &Results) const {
690716
// FIXME: Should this do extra access control filtering?
691717
FORWARD(getDisplayDecls, (Results));

lib/AST/USRGeneration.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
279279
}
280280
}
281281

282-
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
282+
bool ide::printValueDeclUSR(const ValueDecl *D, raw_ostream &OS) {
283283
auto result = evaluateOrDefault(D->getASTContext().evaluator,
284284
USRGenerationRequest { D },
285285
std::string());
@@ -327,17 +327,33 @@ bool ide::printExtensionUSR(const ExtensionDecl *ED, raw_ostream &OS) {
327327
for (auto D : ED->getMembers()) {
328328
if (auto VD = dyn_cast<ValueDecl>(D)) {
329329
OS << getUSRSpacePrefix() << "e:";
330-
return printDeclUSR(VD, OS);
330+
return printValueDeclUSR(VD, OS);
331331
}
332332
}
333333
OS << getUSRSpacePrefix() << "e:";
334-
printDeclUSR(nominal, OS);
334+
printValueDeclUSR(nominal, OS);
335335
for (auto Inherit : ED->getInherited()) {
336336
if (auto T = Inherit.getType()) {
337337
if (T->getAnyNominal())
338-
return printDeclUSR(T->getAnyNominal(), OS);
338+
return printValueDeclUSR(T->getAnyNominal(), OS);
339339
}
340340
}
341341
return true;
342342
}
343343

344+
bool ide::printDeclUSR(const Decl *D, raw_ostream &OS) {
345+
if (D->isImplicit())
346+
return true;
347+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
348+
if (ide::printValueDeclUSR(VD, OS)) {
349+
return true;
350+
}
351+
} else if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
352+
if (ide::printExtensionUSR(ED, OS)) {
353+
return true;
354+
}
355+
} else {
356+
return true;
357+
}
358+
return false;
359+
}

lib/Driver/Driver.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,7 @@ static void chooseModuleAuxiliaryOutputFilePath(Compilation &C,
27932793
StringRef workingDirectory,
27942794
CommandOutput *Output,
27952795
file_types::ID fileID,
2796-
bool isPrivate,
2796+
bool shouldUseProjectFolder = false,
27972797
Optional<options::ID> optId = llvm::None) {
27982798
if (hasExistingAdditionalOutput(*Output, fileID))
27992799
return;
@@ -2819,9 +2819,9 @@ static void chooseModuleAuxiliaryOutputFilePath(Compilation &C,
28192819
bool isTempFile = C.isTemporaryFile(ModulePath);
28202820
auto ModuleName = llvm::sys::path::filename(ModulePath);
28212821
llvm::SmallString<128> Path(llvm::sys::path::parent_path(ModulePath));
2822-
if (isPrivate) {
2823-
llvm::sys::path::append(Path, "Private");
2824-
// If the build system has created a Private dir for us to include the file, use it.
2822+
if (shouldUseProjectFolder) {
2823+
llvm::sys::path::append(Path, "Project");
2824+
// If the build system has created a Project dir for us to include the file, use it.
28252825
if (!llvm::sys::fs::exists(Path)) {
28262826
llvm::sys::path::remove_filename(Path);
28272827
}
@@ -2840,15 +2840,16 @@ void Driver::chooseSwiftSourceInfoOutputPath(Compilation &C,
28402840
CommandOutput *Output) const {
28412841
chooseModuleAuxiliaryOutputFilePath(C, OutputMap, workingDirectory, Output,
28422842
file_types::TY_SwiftSourceInfoFile,
2843-
/*isPrivate*/true, options::OPT_emit_module_source_info_path);
2843+
/*shouldUseProjectFolder*/true,
2844+
options::OPT_emit_module_source_info_path);
28442845
}
28452846

28462847
void Driver::chooseSwiftModuleDocOutputPath(Compilation &C,
28472848
const TypeToPathMap *OutputMap,
28482849
StringRef workingDirectory,
28492850
CommandOutput *Output) const {
28502851
chooseModuleAuxiliaryOutputFilePath(C, OutputMap, workingDirectory, Output,
2851-
file_types::TY_SwiftModuleDocFile, /*isPrivate*/false);
2852+
file_types::TY_SwiftModuleDocFile);
28522853
}
28532854

28542855
void Driver::chooseRemappingOutputPath(Compilation &C,

0 commit comments

Comments
 (0)