@@ -1562,9 +1562,6 @@ static void expectEndOfDirective(PreprocessorDirectiveContext* context)
1562
1562
AdvanceRawToken (context->preprocessor );
1563
1563
}
1564
1564
1565
- // Handle a `#import` directive
1566
- static void HandleImportDirective (PreprocessorDirectiveContext* context);
1567
-
1568
1565
// Handle a `#include` directive
1569
1566
static void HandleIncludeDirective (PreprocessorDirectiveContext* context)
1570
1567
{
@@ -1928,7 +1925,6 @@ static const PreprocessorDirective kDirectives[] =
1928
1925
{ " elif" , &HandleElifDirective, ProcessWhenSkipping },
1929
1926
{ " endif" , &HandleEndIfDirective, ProcessWhenSkipping },
1930
1927
1931
- { " import" , &HandleImportDirective, 0 },
1932
1928
{ " include" , &HandleIncludeDirective, 0 },
1933
1929
{ " define" , &HandleDefineDirective, 0 },
1934
1930
{ " undef" , &HandleUndefDirective, 0 },
@@ -2188,116 +2184,4 @@ TokenList preprocessSource(
2188
2184
return tokens;
2189
2185
}
2190
2186
2191
- //
2192
-
2193
- // Handle a `#import` directive
2194
- static void HandleImportDirective (PreprocessorDirectiveContext* context)
2195
- {
2196
- Token pathToken;
2197
- if (!Expect (context, TokenType::StringLiteral, Diagnostics::expectedTokenInPreprocessorDirective, &pathToken))
2198
- return ;
2199
-
2200
- String path = getFileNameTokenValue (pathToken);
2201
-
2202
- auto directiveLoc = GetDirectiveLoc (context);
2203
- auto expandedDirectiveLoc = context->preprocessor ->translationUnit ->compileRequest ->getSourceManager ()->expandSourceLoc (directiveLoc);
2204
- String pathIncludedFrom = expandedDirectiveLoc.getSpellingPath ();
2205
- String foundPath;
2206
- String foundSource;
2207
-
2208
- IncludeHandler* includeHandler = context->preprocessor ->includeHandler ;
2209
- if (!includeHandler)
2210
- {
2211
- GetSink (context)->diagnose (pathToken.loc , Diagnostics::importFailed, path);
2212
- GetSink (context)->diagnose (pathToken.loc , Diagnostics::noIncludeHandlerSpecified);
2213
- return ;
2214
- }
2215
- auto includeResult = includeHandler->TryToFindIncludeFile (path, pathIncludedFrom, &foundPath, &foundSource);
2216
-
2217
- switch (includeResult)
2218
- {
2219
- case IncludeResult::NotFound:
2220
- case IncludeResult::Error:
2221
- GetSink (context)->diagnose (pathToken.loc , Diagnostics::importFailed, path);
2222
- return ;
2223
-
2224
- case IncludeResult::Found:
2225
- break ;
2226
- }
2227
-
2228
- // Do all checking related to the end of this directive before we push a new stream,
2229
- // just to avoid complications where that check would need to deal with
2230
- // a switch of input stream
2231
- expectEndOfDirective (context);
2232
-
2233
- // TODO: may want to have some kind of canonicalization step here
2234
- String moduleKey = foundPath;
2235
-
2236
- // Import code from the chosen file, if needed. We only
2237
- // need to import on the first `#import` directive, and
2238
- // after that we ignore additional `#import`s for the same file.
2239
- {
2240
- auto translationUnit = context->preprocessor ->translationUnit ;
2241
- auto request = translationUnit->compileRequest ;
2242
-
2243
-
2244
- // Have we already loaded a module matching this name?
2245
- if (request->mapPathToLoadedModule .TryGetValue (moduleKey))
2246
- {
2247
- // The module has already been loaded, so we don't need to
2248
- // actually tokenize the code here. But note that we *do*
2249
- // go on to insert tokens for an `import` operation into
2250
- // the stream, so it is up to downstream code to avoid
2251
- // re-importing the same thing twice.
2252
- }
2253
- else
2254
- {
2255
- // We are going to preprocess the file using the *same* preprocessor
2256
- // state that is already active. The main alternative would be
2257
- // to construct a fresh preprocessor and use that. The current
2258
- // choice is made so that macros defined in the imported file
2259
- // will be made visible to the importer, rather than disappear
2260
- // when a sub-preprocessor gets finalized.
2261
- auto preprocessor = context->preprocessor ;
2262
-
2263
- // We need to save/restore the input stream, so that we can
2264
- // re-use the preprocessor
2265
- PreprocessorInputStream* savedStream = preprocessor->inputStream ;
2266
-
2267
- // Create an input stream for reading from the imported file
2268
- SourceFile* sourceFile = context->preprocessor ->getCompileRequest ()->getSourceManager ()->allocateSourceFile (foundPath, foundSource);
2269
- PreprocessorInputStream* subInputStream = CreateInputStreamForSource (preprocessor, sourceFile);
2270
-
2271
- // Now preprocess that stream
2272
- preprocessor->inputStream = subInputStream;
2273
- TokenList subTokens = ReadAllTokens (preprocessor);
2274
-
2275
- // Restore the previous input stream
2276
- preprocessor->inputStream = savedStream;
2277
-
2278
- // Now we need to do something with those tokens we read
2279
- request->handlePoundImport (
2280
- moduleKey,
2281
- subTokens);
2282
- }
2283
- }
2284
-
2285
- // Now create a dummy token stream to represent the import request,
2286
- // so that it can be manifest in the user's program
2287
-
2288
- Token token;
2289
- token.type = TokenType::PoundImport;
2290
- token.loc = GetDirectiveLoc (context);
2291
- token.flags = 0 ;
2292
- token.Content = foundPath;
2293
-
2294
- SimpleTokenInputStream* inputStream = createSimpleInputStream (
2295
- context->preprocessor ,
2296
- token);
2297
-
2298
- PushInputStream (context->preprocessor , inputStream);
2299
- }
2300
-
2301
-
2302
-
2303
2187
}
0 commit comments