1010//
1111// ===----------------------------------------------------------------------===//
1212
13+ #include " swift/AST/USRGeneration.h"
1314#include " swift/AST/ASTContext.h"
15+ #include " swift/AST/ASTMangler.h"
1416#include " swift/AST/ClangModuleLoader.h"
1517#include " swift/AST/GenericParamList.h"
1618#include " swift/AST/Module.h"
17- #include " swift/AST/USRGeneration.h"
18- #include " swift/AST/ASTMangler.h"
1919#include " swift/AST/SwiftNameTranslation.h"
2020#include " swift/AST/TypeCheckRequests.h"
2121#include " swift/AST/USRGeneration.h"
2222#include " swift/Basic/Assertions.h"
2323#include " swift/Demangling/Demangler.h"
24- #include " llvm/ADT/SmallString.h"
25- #include " llvm/ADT/StringRef.h"
26- #include " llvm/Support/raw_ostream.h"
2724#include " clang/AST/ASTContext.h"
2825#include " clang/AST/Attr.h"
2926#include " clang/Index/USRGeneration.h"
3027#include " clang/Lex/PreprocessingRecord.h"
3128#include " clang/Lex/Preprocessor.h"
29+ #include " llvm/ADT/SmallString.h"
30+ #include " llvm/ADT/StringRef.h"
31+ #include " llvm/Support/raw_ostream.h"
3232
3333using namespace swift ;
3434using namespace ide ;
@@ -170,9 +170,15 @@ static bool shouldUseObjCUSR(const Decl *D) {
170170 return false ;
171171}
172172
173+ void swift::simple_display (llvm::raw_ostream &out,
174+ const USRGenerationOptions &options) {
175+ out << " USRGenerationOptions (distinguishSynthesizedDecls: "
176+ << options.distinguishSynthesizedDecls << " )" ;
177+ }
178+
173179std::string
174- swift::USRGenerationRequest::evaluate (Evaluator &evaluator,
175- const ValueDecl *D ) const {
180+ swift::USRGenerationRequest::evaluate (Evaluator &evaluator, const ValueDecl *D,
181+ USRGenerationOptions options ) const {
176182 if (auto *VD = dyn_cast<VarDecl>(D))
177183 D = VD->getCanonicalVarDecl ();
178184
@@ -184,8 +190,9 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
184190 if (isa<ModuleDecl>(D))
185191 return std::string (); // Ignore.
186192
187- auto interpretAsClangNode = [](const ValueDecl *D)->ClangNode {
188- ClangNode ClangN = D->getClangNode ();
193+ auto interpretAsClangNode = [&options](const ValueDecl *D) -> ClangNode {
194+ auto *importer = D->getASTContext ().getClangModuleLoader ();
195+ ClangNode ClangN = importer->getEffectiveClangNode (D);
189196 if (auto ClangD = ClangN.getAsDecl ()) {
190197 // NSErrorDomain causes the clang enum to be imported like this:
191198 //
@@ -203,12 +210,19 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
203210 // But we want unique USRs for the above symbols, so use the clang USR
204211 // for the enum cases, and the Swift USR for the vars.
205212 //
213+ if (!options.distinguishSynthesizedDecls ) {
214+ return ClangN;
215+ }
206216 if (auto *ClangEnumConst = dyn_cast<clang::EnumConstantDecl>(ClangD)) {
207- if (auto *ClangEnum = dyn_cast<clang::EnumDecl>(ClangEnumConst->getDeclContext ())) {
217+ if (auto *ClangEnum =
218+ dyn_cast<clang::EnumDecl>(ClangEnumConst->getDeclContext ())) {
208219 if (ClangEnum->hasAttr <clang::NSErrorDomainAttr>() && isa<VarDecl>(D))
209220 return ClangNode ();
210221 }
211222 }
223+ if (D->getAttrs ().hasAttribute <ClangImporterSynthesizedTypeAttr>()) {
224+ return ClangNode ();
225+ }
212226 }
213227 return ClangN;
214228 };
@@ -292,10 +306,11 @@ bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
292306 }
293307}
294308
295- bool ide::printValueDeclUSR (const ValueDecl *D, raw_ostream &OS) {
296- auto result = evaluateOrDefault (D->getASTContext ().evaluator ,
297- USRGenerationRequest { D },
298- std::string ());
309+ bool ide::printValueDeclUSR (const ValueDecl *D, raw_ostream &OS,
310+ bool distinguishSynthesizedDecls) {
311+ auto result = evaluateOrDefault (
312+ D->getASTContext ().evaluator ,
313+ USRGenerationRequest{D, {distinguishSynthesizedDecls}}, std::string ());
299314 if (result.empty ())
300315 return true ;
301316 OS << result;
@@ -354,9 +369,10 @@ bool ide::printExtensionUSR(const ExtensionDecl *ED, raw_ostream &OS) {
354369 return true ;
355370}
356371
357- bool ide::printDeclUSR (const Decl *D, raw_ostream &OS) {
372+ bool ide::printDeclUSR (const Decl *D, raw_ostream &OS,
373+ bool distinguishSynthesizedDecls) {
358374 if (auto *VD = dyn_cast<ValueDecl>(D)) {
359- if (ide::printValueDeclUSR (VD, OS)) {
375+ if (ide::printValueDeclUSR (VD, OS, distinguishSynthesizedDecls )) {
360376 return true ;
361377 }
362378 } else if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
0 commit comments