@@ -970,16 +970,7 @@ void CompilerInstance::parseLibraryFile(
970
970
SourceFileKind::Library, implicitImports.kind , BufferID);
971
971
addAdditionalInitialImportsTo (NextInput, implicitImports);
972
972
973
- auto IsPrimary = isWholeModuleCompilation () || isPrimaryInput (BufferID);
974
-
975
- auto &Diags = NextInput->getASTContext ().Diags ;
976
- auto DidSuppressWarnings = Diags.getSuppressWarnings ();
977
- Diags.setSuppressWarnings (DidSuppressWarnings || !IsPrimary);
978
-
979
- parseIntoSourceFile (*NextInput, BufferID, /* DelayedBodyParsing=*/ !IsPrimary);
980
-
981
- Diags.setSuppressWarnings (DidSuppressWarnings);
982
-
973
+ parseIntoSourceFile (*NextInput, BufferID);
983
974
performNameBinding (*NextInput);
984
975
}
985
976
@@ -1023,8 +1014,7 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
1023
1014
Diags.setSuppressWarnings (DidSuppressWarnings || !mainIsPrimary);
1024
1015
1025
1016
// Parse the Swift decls into the source file.
1026
- parseIntoSourceFile (MainFile, MainBufferID,
1027
- /* delayBodyParsing*/ !mainIsPrimary);
1017
+ parseIntoSourceFile (MainFile, MainBufferID);
1028
1018
1029
1019
// For a primary, also perform type checking if needed. Otherwise, just do
1030
1020
// name binding.
@@ -1081,17 +1071,27 @@ void CompilerInstance::finishTypeChecking() {
1081
1071
1082
1072
SourceFile *CompilerInstance::createSourceFileForMainModule (
1083
1073
SourceFileKind fileKind, SourceFile::ImplicitModuleImportKind importKind,
1084
- Optional<unsigned > bufferID) {
1074
+ Optional<unsigned > bufferID, SourceFile::ParsingOptions opts ) {
1085
1075
ModuleDecl *mainModule = getMainModule ();
1076
+
1077
+ auto isPrimary = bufferID && isPrimaryInput (*bufferID);
1078
+ if (isPrimary || isWholeModuleCompilation ()) {
1079
+ // Disable delayed body parsing for primaries.
1080
+ opts |= SourceFile::ParsingFlags::DisableDelayedBodies;
1081
+ } else {
1082
+ // Suppress parse warnings for non-primaries, as they'll get parsed multiple
1083
+ // times.
1084
+ opts |= SourceFile::ParsingFlags::SuppressWarnings;
1085
+ }
1086
+
1086
1087
SourceFile *inputFile = new (*Context)
1087
1088
SourceFile (*mainModule, fileKind, bufferID, importKind,
1088
1089
Invocation.getLangOptions ().CollectParsedToken ,
1089
- Invocation.getLangOptions ().BuildSyntaxTree );
1090
+ Invocation.getLangOptions ().BuildSyntaxTree , opts );
1090
1091
MainModule->addFile (*inputFile);
1091
1092
1092
- if (bufferID && isPrimaryInput (*bufferID)) {
1093
+ if (isPrimary)
1093
1094
recordPrimarySourceFile (inputFile);
1094
- }
1095
1095
1096
1096
if (bufferID == SourceMgr.getCodeCompletionBufferID ()) {
1097
1097
assert (!CodeCompletionFile && " Multiple code completion files?" );
@@ -1113,35 +1113,32 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
1113
1113
" only supports parsing .swift files" );
1114
1114
(void )Kind;
1115
1115
1116
+ SourceFile::ParsingOptions parsingOpts;
1117
+ if (!EvaluateConditionals)
1118
+ parsingOpts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
1119
+ if (!CanDelayBodies)
1120
+ parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
1121
+
1116
1122
// Make sure the main file is the first file in the module but parse it last,
1117
1123
// to match the parsing logic used when performing Sema.
1118
1124
if (MainBufferID != NO_SUCH_BUFFER) {
1119
1125
assert (Kind == InputFileKind::Swift ||
1120
1126
Kind == InputFileKind::SwiftModuleInterface);
1121
1127
createSourceFileForMainModule (Invocation.getSourceFileKind (),
1122
1128
SourceFile::ImplicitModuleImportKind::None,
1123
- MainBufferID);
1129
+ MainBufferID, parsingOpts );
1124
1130
}
1125
1131
1126
- auto shouldDelayBodies = [&](unsigned bufferID) -> bool {
1127
- if (!CanDelayBodies)
1128
- return false ;
1129
-
1130
- // Don't delay bodies in whole module mode or for primary files.
1131
- return !(isWholeModuleCompilation () || isPrimaryInput (bufferID));
1132
- };
1133
-
1134
1132
// Parse all the library files.
1135
1133
for (auto BufferID : InputSourceCodeBufferIDs) {
1136
1134
if (BufferID == MainBufferID)
1137
1135
continue ;
1138
1136
1139
1137
SourceFile *NextInput = createSourceFileForMainModule (
1140
1138
SourceFileKind::Library, SourceFile::ImplicitModuleImportKind::None,
1141
- BufferID);
1139
+ BufferID, parsingOpts );
1142
1140
1143
- parseIntoSourceFile (*NextInput, BufferID, shouldDelayBodies (BufferID),
1144
- EvaluateConditionals);
1141
+ parseIntoSourceFile (*NextInput, BufferID);
1145
1142
}
1146
1143
1147
1144
// Now parse the main file.
@@ -1151,8 +1148,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
1151
1148
MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
1152
1149
assert (MainBufferID == MainFile.getBufferID ());
1153
1150
1154
- parseIntoSourceFile (MainFile, MainBufferID, shouldDelayBodies (MainBufferID),
1155
- EvaluateConditionals);
1151
+ parseIntoSourceFile (MainFile, MainBufferID);
1156
1152
}
1157
1153
1158
1154
assert (Context->LoadedModules .size () == 1 &&
0 commit comments