Skip to content

Commit 6783d24

Browse files
authored
Merge pull request #13916 from akyrtzi/fix-clang-headers-order (#14086)
[ClangImporter] Make sure that headers from the bridging header are considered 'isBeforeInTranslationUnit' compared to headers imported from swift code (cherry picked from commit f0c0405) rdar://problem/36765929
1 parent 0caf244 commit 6783d24

File tree

10 files changed

+63
-12
lines changed

10 files changed

+63
-12
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,17 @@ class PrintingCodeCompletionConsumer
856856
: public SimpleCachingCodeCompletionConsumer {
857857
llvm::raw_ostream &OS;
858858
bool IncludeKeywords;
859+
bool IncludeComments;
859860

860861
public:
861-
PrintingCodeCompletionConsumer(llvm::raw_ostream &OS, bool IncludeKeywords = true)
862-
: OS(OS), IncludeKeywords(IncludeKeywords) {
863-
}
864-
865-
void handleResults(
866-
MutableArrayRef<CodeCompletionResult *> Results) override;
862+
PrintingCodeCompletionConsumer(llvm::raw_ostream &OS,
863+
bool IncludeKeywords = true,
864+
bool IncludeComments = true)
865+
: OS(OS),
866+
IncludeKeywords(IncludeKeywords),
867+
IncludeComments(IncludeComments) {}
868+
869+
void handleResults(MutableArrayRef<CodeCompletionResult *> Results) override;
867870
};
868871

869872
/// \brief Create a factory for code completion callbacks.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,12 +1478,17 @@ ModuleDecl *ClangImporter::loadModule(
14781478
// invalid, it can't be the same thing twice in a row, and it has to come
14791479
// from an actual buffer, so we make a fake buffer and just use a counter.
14801480
if (!Impl.DummyImportBuffer.isValid()) {
1481+
clang::SourceLocation includeLoc =
1482+
srcMgr.getLocForStartOfFile(srcMgr.getMainFileID());
1483+
// Zero offset is reserved for the bridging header. Increase the offset
1484+
// so that headers from the bridging header are considered as coming
1485+
// before headers that are imported from swift code.
1486+
includeLoc = includeLoc.getLocWithOffset(1);
14811487
Impl.DummyImportBuffer = srcMgr.createFileID(
14821488
llvm::make_unique<ZeroFilledMemoryBuffer>(
14831489
256*1024, StringRef(Implementation::moduleImportBufferName)),
14841490
clang::SrcMgr::C_User,
1485-
/*LoadedID*/0, /*LoadedOffset*/0,
1486-
srcMgr.getLocForStartOfFile(srcMgr.getMainFileID()));
1491+
/*LoadedID*/0, /*LoadedOffset*/0, includeLoc);
14871492
}
14881493
clang::SourceLocation clangImportLoc
14891494
= srcMgr.getLocForStartOfFile(Impl.DummyImportBuffer)

lib/IDE/CodeCompletion.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,6 +5514,11 @@ void PrintingCodeCompletionConsumer::handleResults(
55145514
Result->getCompletionString()->getName(NameOs);
55155515
OS << "; name=" << Name;
55165516

5517+
StringRef comment = Result->getBriefDocComment();
5518+
if (IncludeComments && !comment.empty()) {
5519+
OS << "; comment=" << comment;
5520+
}
5521+
55175522
OS << "\n";
55185523
}
55195524
OS << "End completions\n";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import somemod1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module somemod1 {
2+
header "somemod1.h"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// some_func11 is cool function.
2+
void some_func11(void);
3+
4+
/// some_func12 is cool function.
5+
void some_func12(void);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module somemod2 {
2+
header "somemod2.h"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// some_func21 is cool function.
2+
void some_func21(void);
3+
4+
/// some_func22 is cool function.
5+
void some_func22(void);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TOP -code-completion-comments=true \
2+
// RUN: -import-objc-header %S/Inputs/bridge.h -I %S/Inputs/somemod1 -I %S/Inputs/somemod2 | %FileCheck %s -check-prefix=CHECK-TOP
3+
4+
// REQUIRES: objc_interop
5+
6+
import somemod2
7+
8+
#^TOP^#
9+
// CHECK-TOP: name=some_func11(); comment=some_func11 is cool function.
10+
// CHECK-TOP: name=some_func12(); comment=some_func12 is cool function.
11+
// CHECK-TOP: name=some_func21(); comment=some_func21 is cool function.
12+
// CHECK-TOP: name=some_func22(); comment=some_func22 is cool function.

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ CodeCompletionKeywords("code-completion-keywords",
396396
llvm::cl::cat(Category),
397397
llvm::cl::init(true));
398398

399+
static llvm::cl::opt<bool>
400+
CodeCompletionComments("code-completion-comments",
401+
llvm::cl::desc("Include comments in code completion results"),
402+
llvm::cl::cat(Category),
403+
llvm::cl::init(false));
404+
399405
static llvm::cl::opt<std::string>
400406
DebugClientDiscriminator("debug-client-discriminator",
401407
llvm::cl::desc("A discriminator to prefer in lookups"),
@@ -671,7 +677,8 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
671677
StringRef SecondSourceFileName,
672678
StringRef CodeCompletionToken,
673679
bool CodeCompletionDiagnostics,
674-
bool CodeCompletionKeywords) {
680+
bool CodeCompletionKeywords,
681+
bool CodeCompletionComments) {
675682
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
676683
llvm::MemoryBuffer::getFile(SourceFilename);
677684
if (!FileBufOrErr) {
@@ -712,7 +719,7 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
712719
// Create a CodeCompletionConsumer.
713720
std::unique_ptr<ide::CodeCompletionConsumer> Consumer(
714721
new ide::PrintingCodeCompletionConsumer(
715-
llvm::outs(), CodeCompletionKeywords));
722+
llvm::outs(), CodeCompletionKeywords, CodeCompletionComments));
716723

717724
// Create a factory for code completion callbacks that will feed the
718725
// Consumer.
@@ -2960,7 +2967,8 @@ int main(int argc, char *argv[]) {
29602967
}
29612968

29622969
ide::PrintingCodeCompletionConsumer Consumer(
2963-
llvm::outs(), options::CodeCompletionKeywords);
2970+
llvm::outs(), options::CodeCompletionKeywords,
2971+
options::CodeCompletionComments);
29642972
for (StringRef filename : options::InputFilenames) {
29652973
auto resultsOpt = ide::OnDiskCodeCompletionCache::getFromFile(filename);
29662974
if (!resultsOpt) {
@@ -3152,7 +3160,8 @@ int main(int argc, char *argv[]) {
31523160
options::SecondSourceFilename,
31533161
options::CodeCompletionToken,
31543162
options::CodeCompletionDiagnostics,
3155-
options::CodeCompletionKeywords);
3163+
options::CodeCompletionKeywords,
3164+
options::CodeCompletionComments);
31563165
break;
31573166

31583167
case ActionType::REPLCodeCompletion:

0 commit comments

Comments
 (0)