clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaModule.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaModule.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/AST/ASTConsumer.h" |
15 | #include "clang/Lex/HeaderSearch.h" |
16 | #include "clang/Lex/Preprocessor.h" |
17 | #include "clang/Sema/SemaInternal.h" |
18 | |
19 | using namespace clang; |
20 | using namespace sema; |
21 | |
22 | static void checkModuleImportContext(Sema &S, Module *M, |
23 | SourceLocation ImportLoc, DeclContext *DC, |
24 | bool FromInclude = false) { |
25 | SourceLocation ExternCLoc; |
26 | |
27 | if (auto *LSD = dyn_cast<LinkageSpecDecl>(DC)) { |
28 | switch (LSD->getLanguage()) { |
29 | case LinkageSpecDecl::lang_c: |
30 | if (ExternCLoc.isInvalid()) |
31 | ExternCLoc = LSD->getBeginLoc(); |
32 | break; |
33 | case LinkageSpecDecl::lang_cxx: |
34 | break; |
35 | } |
36 | DC = LSD->getParent(); |
37 | } |
38 | |
39 | while (isa<LinkageSpecDecl>(DC) || isa<ExportDecl>(DC)) |
40 | DC = DC->getParent(); |
41 | |
42 | if (!isa<TranslationUnitDecl>(DC)) { |
43 | S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M)) |
44 | ? diag::ext_module_import_not_at_top_level_noop |
45 | : diag::err_module_import_not_at_top_level_fatal) |
46 | << M->getFullModuleName() << DC; |
47 | S.Diag(cast<Decl>(DC)->getBeginLoc(), |
48 | diag::note_module_import_not_at_top_level) |
49 | << DC; |
50 | } else if (!M->IsExternC && ExternCLoc.isValid()) { |
51 | S.Diag(ImportLoc, diag::ext_module_import_in_extern_c) |
52 | << M->getFullModuleName(); |
53 | S.Diag(ExternCLoc, diag::note_extern_c_begins_here); |
54 | } |
55 | } |
56 | |
57 | Sema::DeclGroupPtrTy |
58 | Sema::ActOnGlobalModuleFragmentDecl(SourceLocation ModuleLoc) { |
59 | if (!ModuleScopes.empty() && |
60 | ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment) { |
61 | |
62 | |
63 | assert(getLangOpts().CPlusPlusModules && getLangOpts().ModulesTS && |
64 | "unexpectedly encountered multiple global module fragment decls"); |
65 | ModuleScopes.back().BeginLoc = ModuleLoc; |
66 | return nullptr; |
67 | } |
68 | |
69 | |
70 | |
71 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
72 | auto *GlobalModule = Map.createGlobalModuleFragmentForModuleUnit(ModuleLoc); |
73 | assert(GlobalModule && "module creation should not fail"); |
74 | |
75 | |
76 | ModuleScopes.push_back({}); |
77 | ModuleScopes.back().BeginLoc = ModuleLoc; |
78 | ModuleScopes.back().Module = GlobalModule; |
79 | VisibleModules.setVisible(GlobalModule, ModuleLoc); |
80 | |
81 | |
82 | auto *TU = Context.getTranslationUnitDecl(); |
83 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::Visible); |
84 | TU->setLocalOwningModule(GlobalModule); |
85 | |
86 | |
87 | return nullptr; |
88 | } |
89 | |
90 | Sema::DeclGroupPtrTy |
91 | Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, |
92 | ModuleDeclKind MDK, ModuleIdPath Path, bool IsFirstDecl) { |
93 | assert((getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) && |
94 | "should only have module decl in Modules TS or C++20"); |
95 | |
96 | |
97 | |
98 | |
99 | switch (getLangOpts().getCompilingModule()) { |
100 | case LangOptions::CMK_None: |
101 | |
102 | break; |
103 | |
104 | case LangOptions::CMK_ModuleInterface: |
105 | if (MDK != ModuleDeclKind::Implementation) |
106 | break; |
107 | |
108 | |
109 | |
110 | Diag(ModuleLoc, diag::err_module_interface_implementation_mismatch) |
111 | << FixItHint::CreateInsertion(ModuleLoc, "export "); |
112 | MDK = ModuleDeclKind::Interface; |
113 | break; |
114 | |
115 | case LangOptions::CMK_ModuleMap: |
116 | Diag(ModuleLoc, diag::err_module_decl_in_module_map_module); |
117 | return nullptr; |
118 | |
119 | case LangOptions::CMK_HeaderModule: |
120 | Diag(ModuleLoc, diag::err_module_decl_in_header_module); |
121 | return nullptr; |
122 | } |
123 | |
124 | assert(ModuleScopes.size() <= 1 && "expected to be at global module scope"); |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | if (!ModuleScopes.empty() && |
131 | ModuleScopes.back().Module->isModulePurview()) { |
132 | Diag(ModuleLoc, diag::err_module_redeclaration); |
133 | Diag(VisibleModules.getImportLoc(ModuleScopes.back().Module), |
134 | diag::note_prev_module_declaration); |
135 | return nullptr; |
136 | } |
137 | |
138 | |
139 | Module *GlobalModuleFragment = nullptr; |
140 | if (!ModuleScopes.empty() && |
141 | ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment) |
142 | GlobalModuleFragment = ModuleScopes.back().Module; |
143 | |
144 | |
145 | |
146 | if (getLangOpts().CPlusPlusModules && !IsFirstDecl && !GlobalModuleFragment) { |
147 | Diag(ModuleLoc, diag::err_module_decl_not_at_start); |
148 | SourceLocation BeginLoc = |
149 | ModuleScopes.empty() |
150 | ? SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()) |
151 | : ModuleScopes.back().BeginLoc; |
152 | if (BeginLoc.isValid()) { |
153 | Diag(BeginLoc, diag::note_global_module_introducer_missing) |
154 | << FixItHint::CreateInsertion(BeginLoc, "module;\n"); |
155 | } |
156 | } |
157 | |
158 | |
159 | |
160 | |
161 | std::string ModuleName; |
162 | for (auto &Piece : Path) { |
163 | if (!ModuleName.empty()) |
164 | ModuleName += "."; |
165 | ModuleName += Piece.first->getName(); |
166 | } |
167 | |
168 | |
169 | |
170 | if (!getLangOpts().CurrentModule.empty() && |
171 | getLangOpts().CurrentModule != ModuleName) { |
172 | Diag(Path.front().second, diag::err_current_module_name_mismatch) |
173 | << SourceRange(Path.front().second, Path.back().second) |
174 | << getLangOpts().CurrentModule; |
175 | return nullptr; |
176 | } |
177 | const_cast<LangOptions&>(getLangOpts()).CurrentModule = ModuleName; |
178 | |
179 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
180 | Module *Mod; |
181 | |
182 | switch (MDK) { |
183 | case ModuleDeclKind::Interface: { |
184 | |
185 | |
186 | if (auto *M = Map.findModule(ModuleName)) { |
187 | Diag(Path[0].second, diag::err_module_redefinition) << ModuleName; |
188 | if (M->DefinitionLoc.isValid()) |
189 | Diag(M->DefinitionLoc, diag::note_prev_module_definition); |
190 | else if (Optional<FileEntryRef> FE = M->getASTFile()) |
191 | Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file) |
192 | << FE->getName(); |
193 | Mod = M; |
194 | break; |
195 | } |
196 | |
197 | |
198 | Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName, |
199 | GlobalModuleFragment); |
200 | assert(Mod && "module creation should not fail"); |
201 | break; |
202 | } |
203 | |
204 | case ModuleDeclKind::Implementation: |
205 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc( |
206 | PP.getIdentifierInfo(ModuleName), Path[0].second); |
207 | Mod = getModuleLoader().loadModule(ModuleLoc, {ModuleNameLoc}, |
208 | Module::AllVisible, |
209 | false); |
210 | if (!Mod) { |
211 | Diag(ModuleLoc, diag::err_module_not_defined) << ModuleName; |
212 | |
213 | Mod = Map.createModuleForInterfaceUnit(ModuleLoc, ModuleName, |
214 | GlobalModuleFragment); |
215 | } |
216 | break; |
217 | } |
218 | |
219 | if (!GlobalModuleFragment) { |
220 | ModuleScopes.push_back({}); |
221 | if (getLangOpts().ModulesLocalVisibility) |
222 | ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
223 | } else { |
224 | |
225 | ActOnEndOfTranslationUnitFragment(TUFragmentKind::Global); |
226 | } |
227 | |
228 | |
229 | ModuleScopes.back().BeginLoc = StartLoc; |
230 | ModuleScopes.back().Module = Mod; |
231 | ModuleScopes.back().ModuleInterface = MDK != ModuleDeclKind::Implementation; |
232 | VisibleModules.setVisible(Mod, ModuleLoc); |
233 | |
234 | |
235 | |
236 | |
237 | auto *TU = Context.getTranslationUnitDecl(); |
238 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate); |
239 | TU->setLocalOwningModule(Mod); |
240 | |
241 | |
242 | return nullptr; |
243 | } |
244 | |
245 | Sema::DeclGroupPtrTy |
246 | Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc, |
247 | SourceLocation PrivateLoc) { |
248 | |
249 | |
250 | |
251 | switch (ModuleScopes.empty() ? Module::GlobalModuleFragment |
252 | : ModuleScopes.back().Module->Kind) { |
253 | case Module::ModuleMapModule: |
254 | case Module::GlobalModuleFragment: |
255 | Diag(PrivateLoc, diag::err_private_module_fragment_not_module); |
256 | return nullptr; |
257 | |
258 | case Module::PrivateModuleFragment: |
259 | Diag(PrivateLoc, diag::err_private_module_fragment_redefined); |
260 | Diag(ModuleScopes.back().BeginLoc, diag::note_previous_definition); |
261 | return nullptr; |
262 | |
263 | case Module::ModuleInterfaceUnit: |
264 | break; |
265 | } |
266 | |
267 | if (!ModuleScopes.back().ModuleInterface) { |
268 | Diag(PrivateLoc, diag::err_private_module_fragment_not_module_interface); |
269 | Diag(ModuleScopes.back().BeginLoc, |
270 | diag::note_not_module_interface_add_export) |
271 | << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
272 | return nullptr; |
273 | } |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | ActOnEndOfTranslationUnitFragment(TUFragmentKind::Normal); |
282 | |
283 | auto &Map = PP.getHeaderSearchInfo().getModuleMap(); |
284 | Module *PrivateModuleFragment = |
285 | Map.createPrivateModuleFragmentForInterfaceUnit( |
286 | ModuleScopes.back().Module, PrivateLoc); |
287 | assert(PrivateModuleFragment && "module creation should not fail"); |
288 | |
289 | |
290 | ModuleScopes.push_back({}); |
291 | ModuleScopes.back().BeginLoc = ModuleLoc; |
292 | ModuleScopes.back().Module = PrivateModuleFragment; |
293 | ModuleScopes.back().ModuleInterface = true; |
294 | VisibleModules.setVisible(PrivateModuleFragment, ModuleLoc); |
295 | |
296 | |
297 | |
298 | |
299 | auto *TU = Context.getTranslationUnitDecl(); |
300 | TU->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate); |
301 | TU->setLocalOwningModule(PrivateModuleFragment); |
302 | |
303 | |
304 | return nullptr; |
305 | } |
306 | |
307 | DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
308 | SourceLocation ExportLoc, |
309 | SourceLocation ImportLoc, |
310 | ModuleIdPath Path) { |
311 | |
312 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; |
313 | if (getLangOpts().ModulesTS) { |
314 | std::string ModuleName; |
315 | for (auto &Piece : Path) { |
316 | if (!ModuleName.empty()) |
317 | ModuleName += "."; |
318 | ModuleName += Piece.first->getName(); |
319 | } |
320 | ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second}; |
321 | Path = ModuleIdPath(ModuleNameLoc); |
322 | } |
323 | |
324 | Module *Mod = |
325 | getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
326 | false); |
327 | if (!Mod) |
328 | return true; |
329 | |
330 | return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path); |
331 | } |
332 | |
333 | |
334 | static const ExportDecl *getEnclosingExportDecl(const Decl *D) { |
335 | for (auto *DC = D->getLexicalDeclContext(); DC; DC = DC->getLexicalParent()) |
336 | if (auto *ED = dyn_cast<ExportDecl>(DC)) |
337 | return ED; |
338 | return nullptr; |
339 | } |
340 | |
341 | DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, |
342 | SourceLocation ExportLoc, |
343 | SourceLocation ImportLoc, |
344 | Module *Mod, ModuleIdPath Path) { |
345 | VisibleModules.setVisible(Mod, ImportLoc); |
346 | |
347 | checkModuleImportContext(*this, Mod, ImportLoc, CurContext); |
348 | |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule && |
357 | (getLangOpts().isCompilingModule() || !getLangOpts().ModulesTS)) { |
358 | Diag(ImportLoc, getLangOpts().isCompilingModule() |
359 | ? diag::err_module_self_import |
360 | : diag::err_module_import_in_implementation) |
361 | << Mod->getFullModuleName() << getLangOpts().CurrentModule; |
362 | } |
363 | |
364 | SmallVector<SourceLocation, 2> IdentifierLocs; |
365 | Module *ModCheck = Mod; |
366 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
367 | |
368 | |
369 | if (!ModCheck) |
370 | break; |
371 | ModCheck = ModCheck->Parent; |
372 | |
373 | IdentifierLocs.push_back(Path[I].second); |
374 | } |
375 | |
376 | |
377 | |
378 | if (Path.empty()) { |
379 | for (; ModCheck; ModCheck = ModCheck->Parent) { |
380 | IdentifierLocs.push_back(SourceLocation()); |
381 | } |
382 | } |
383 | |
384 | ImportDecl *Import = ImportDecl::Create(Context, CurContext, StartLoc, |
385 | Mod, IdentifierLocs); |
386 | CurContext->addDecl(Import); |
387 | |
388 | |
389 | |
390 | if (!ModuleScopes.empty()) |
391 | Context.addModuleInitializer(ModuleScopes.back().Module, Import); |
392 | |
393 | |
394 | if (!ModuleScopes.empty() && ModuleScopes.back().ModuleInterface) { |
395 | if (ExportLoc.isValid() || getEnclosingExportDecl(Import)) |
396 | getCurrentModule()->Exports.emplace_back(Mod, false); |
397 | } else if (ExportLoc.isValid()) { |
398 | Diag(ExportLoc, diag::err_export_not_in_module_interface); |
399 | } |
400 | |
401 | return Import; |
402 | } |
403 | |
404 | void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
405 | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
406 | BuildModuleInclude(DirectiveLoc, Mod); |
407 | } |
408 | |
409 | void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { |
410 | |
411 | |
412 | |
413 | |
414 | |
415 | bool IsInModuleIncludes = |
416 | TUKind == TU_Module && |
417 | getSourceManager().isWrittenInMainFile(DirectiveLoc); |
418 | |
419 | bool ShouldAddImport = !IsInModuleIncludes; |
420 | |
421 | |
422 | |
423 | if (ShouldAddImport) { |
424 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
425 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
426 | DirectiveLoc, Mod, |
427 | DirectiveLoc); |
428 | if (!ModuleScopes.empty()) |
429 | Context.addModuleInitializer(ModuleScopes.back().Module, ImportD); |
430 | TU->addDecl(ImportD); |
431 | Consumer.HandleImplicitImportDecl(ImportD); |
432 | } |
433 | |
434 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, DirectiveLoc); |
435 | VisibleModules.setVisible(Mod, DirectiveLoc); |
436 | } |
437 | |
438 | void Sema::ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod) { |
439 | checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); |
440 | |
441 | ModuleScopes.push_back({}); |
442 | ModuleScopes.back().Module = Mod; |
443 | if (getLangOpts().ModulesLocalVisibility) |
444 | ModuleScopes.back().OuterVisibleModules = std::move(VisibleModules); |
445 | |
446 | VisibleModules.setVisible(Mod, DirectiveLoc); |
447 | |
448 | |
449 | |
450 | |
451 | if (getLangOpts().trackLocalOwningModule()) { |
452 | for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
453 | cast<Decl>(DC)->setModuleOwnershipKind( |
454 | getLangOpts().ModulesLocalVisibility |
455 | ? Decl::ModuleOwnershipKind::VisibleWhenImported |
456 | : Decl::ModuleOwnershipKind::Visible); |
457 | cast<Decl>(DC)->setLocalOwningModule(Mod); |
458 | } |
459 | } |
460 | } |
461 | |
462 | void Sema::ActOnModuleEnd(SourceLocation EomLoc, Module *Mod) { |
463 | if (getLangOpts().ModulesLocalVisibility) { |
464 | VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules); |
465 | |
466 | |
467 | VisibleNamespaceCache.clear(); |
468 | } |
469 | |
470 | assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod && |
471 | "left the wrong module scope"); |
472 | ModuleScopes.pop_back(); |
473 | |
474 | |
475 | |
476 | FileID File = getSourceManager().getFileID(EomLoc); |
477 | SourceLocation DirectiveLoc; |
478 | if (EomLoc == getSourceManager().getLocForEndOfFile(File)) { |
479 | |
480 | assert(File != getSourceManager().getMainFileID() && |
481 | "end of submodule in main source file"); |
482 | DirectiveLoc = getSourceManager().getIncludeLoc(File); |
483 | } else { |
484 | |
485 | DirectiveLoc = EomLoc; |
486 | } |
487 | BuildModuleInclude(DirectiveLoc, Mod); |
488 | |
489 | |
490 | if (getLangOpts().trackLocalOwningModule()) { |
491 | |
492 | |
493 | for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
494 | cast<Decl>(DC)->setLocalOwningModule(getCurrentModule()); |
495 | if (!getCurrentModule()) |
496 | cast<Decl>(DC)->setModuleOwnershipKind( |
497 | Decl::ModuleOwnershipKind::Unowned); |
498 | } |
499 | } |
500 | } |
501 | |
502 | void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, |
503 | Module *Mod) { |
504 | |
505 | if (isSFINAEContext() || !getLangOpts().ModulesErrorRecovery || |
506 | VisibleModules.isVisible(Mod)) |
507 | return; |
508 | |
509 | |
510 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
511 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
512 | Loc, Mod, Loc); |
513 | TU->addDecl(ImportD); |
514 | Consumer.HandleImplicitImportDecl(ImportD); |
515 | |
516 | |
517 | getModuleLoader().makeModuleVisible(Mod, Module::AllVisible, Loc); |
518 | VisibleModules.setVisible(Mod, Loc); |
519 | } |
520 | |
521 | |
522 | |
523 | Decl *Sema::ActOnStartExportDecl(Scope *S, SourceLocation ExportLoc, |
524 | SourceLocation LBraceLoc) { |
525 | ExportDecl *D = ExportDecl::Create(Context, CurContext, ExportLoc); |
526 | |
527 | |
528 | D->setRBraceLoc(LBraceLoc); |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | if (ModuleScopes.empty() || !ModuleScopes.back().Module->isModulePurview()) { |
| |
535 | Diag(ExportLoc, diag::err_export_not_in_module_interface) << 0; |
536 | } else if (!ModuleScopes.back().ModuleInterface) { |
| 2 | | Assuming field 'ModuleInterface' is true | |
|
| |
537 | Diag(ExportLoc, diag::err_export_not_in_module_interface) << 1; |
538 | Diag(ModuleScopes.back().BeginLoc, |
539 | diag::note_not_module_interface_add_export) |
540 | << FixItHint::CreateInsertion(ModuleScopes.back().BeginLoc, "export "); |
541 | } else if (ModuleScopes.back().Module->Kind == |
| 4 | | Value assigned to field 'CurContext' | |
|
| 5 | | Assuming field 'Kind' is not equal to PrivateModuleFragment | |
|
| |
542 | Module::PrivateModuleFragment) { |
543 | Diag(ExportLoc, diag::err_export_in_private_module_fragment); |
544 | Diag(ModuleScopes.back().BeginLoc, diag::note_private_module_fragment); |
545 | } |
546 | |
547 | for (const DeclContext *DC = CurContext; DC; DC = DC->getLexicalParent()) { |
| 7 | | Assuming pointer value is null | |
|
| 8 | | Loop condition is false. Execution continues on line 571 | |
|
548 | if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
549 | |
550 | |
551 | if (ND->isAnonymousNamespace()) { |
552 | Diag(ExportLoc, diag::err_export_within_anonymous_namespace); |
553 | Diag(ND->getLocation(), diag::note_anonymous_namespace); |
554 | |
555 | D->setInvalidDecl(); |
556 | break; |
557 | } |
558 | |
559 | |
560 | |
561 | |
562 | |
563 | |
564 | if (!DeferredExportedNamespaces.insert(ND).second) |
565 | break; |
566 | } |
567 | } |
568 | |
569 | |
570 | |
571 | if (auto *ED = getEnclosingExportDecl(D)) { |
| |
| |
572 | Diag(ExportLoc, diag::err_export_within_export); |
573 | if (ED->hasBraces()) |
574 | Diag(ED->getLocation(), diag::note_export); |
575 | } |
576 | |
577 | CurContext->addDecl(D); |
| 11 | | Called C++ object pointer is null |
|
578 | PushDeclContext(S, D); |
579 | D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); |
580 | return D; |
581 | } |
582 | |
583 | static bool checkExportedDeclContext(Sema &S, DeclContext *DC, |
584 | SourceLocation BlockStart); |
585 | |
586 | namespace { |
587 | enum class UnnamedDeclKind { |
588 | Empty, |
589 | StaticAssert, |
590 | Asm, |
591 | UsingDirective, |
592 | Context |
593 | }; |
594 | } |
595 | |
596 | static llvm::Optional<UnnamedDeclKind> getUnnamedDeclKind(Decl *D) { |
597 | if (isa<EmptyDecl>(D)) |
598 | return UnnamedDeclKind::Empty; |
599 | if (isa<StaticAssertDecl>(D)) |
600 | return UnnamedDeclKind::StaticAssert; |
601 | if (isa<FileScopeAsmDecl>(D)) |
602 | return UnnamedDeclKind::Asm; |
603 | if (isa<UsingDirectiveDecl>(D)) |
604 | return UnnamedDeclKind::UsingDirective; |
605 | |
606 | return llvm::None; |
607 | } |
608 | |
609 | unsigned getUnnamedDeclDiag(UnnamedDeclKind UDK, bool InBlock) { |
610 | switch (UDK) { |
611 | case UnnamedDeclKind::Empty: |
612 | case UnnamedDeclKind::StaticAssert: |
613 | |
614 | |
615 | return InBlock ? diag::ext_export_no_name_block : diag::err_export_no_name; |
616 | |
617 | case UnnamedDeclKind::UsingDirective: |
618 | |
619 | return diag::ext_export_using_directive; |
620 | |
621 | case UnnamedDeclKind::Context: |
622 | |
623 | |
624 | return diag::ext_export_no_names; |
625 | |
626 | case UnnamedDeclKind::Asm: |
627 | return diag::err_export_no_name; |
628 | } |
629 | llvm_unreachable("unknown kind"); |
630 | } |
631 | |
632 | static void diagExportedUnnamedDecl(Sema &S, UnnamedDeclKind UDK, Decl *D, |
633 | SourceLocation BlockStart) { |
634 | S.Diag(D->getLocation(), getUnnamedDeclDiag(UDK, BlockStart.isValid())) |
635 | << (unsigned)UDK; |
636 | if (BlockStart.isValid()) |
637 | S.Diag(BlockStart, diag::note_export); |
638 | } |
639 | |
640 | |
641 | static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) { |
642 | |
643 | |
644 | if (auto UDK = getUnnamedDeclKind(D)) |
645 | diagExportedUnnamedDecl(S, *UDK, D, BlockStart); |
646 | |
647 | |
648 | if (auto *ND = dyn_cast<NamedDecl>(D)) { |
649 | |
650 | |
651 | if (ND->getDeclName() && ND->getFormalLinkage() == InternalLinkage) { |
652 | S.Diag(ND->getLocation(), diag::err_export_internal) << ND; |
653 | if (BlockStart.isValid()) |
654 | S.Diag(BlockStart, diag::note_export); |
655 | } |
656 | } |
657 | |
658 | |
659 | |
660 | |
661 | if (auto *USD = dyn_cast<UsingShadowDecl>(D)) { |
662 | NamedDecl *Target = USD->getUnderlyingDecl(); |
663 | if (Target->getFormalLinkage() == InternalLinkage) { |
664 | S.Diag(USD->getLocation(), diag::err_export_using_internal) << Target; |
665 | S.Diag(Target->getLocation(), diag::note_using_decl_target); |
666 | if (BlockStart.isValid()) |
667 | S.Diag(BlockStart, diag::note_export); |
668 | } |
669 | } |
670 | |
671 | |
672 | |
673 | if (auto *DC = dyn_cast<DeclContext>(D)) |
674 | if (DC->getRedeclContext()->isFileContext() && !isa<EnumDecl>(D)) |
675 | return checkExportedDeclContext(S, DC, BlockStart); |
676 | return false; |
677 | } |
678 | |
679 | |
680 | static bool checkExportedDeclContext(Sema &S, DeclContext *DC, |
681 | SourceLocation BlockStart) { |
682 | bool AllUnnamed = true; |
683 | for (auto *D : DC->decls()) |
684 | AllUnnamed &= checkExportedDecl(S, D, BlockStart); |
685 | return AllUnnamed; |
686 | } |
687 | |
688 | |
689 | Decl *Sema::ActOnFinishExportDecl(Scope *S, Decl *D, SourceLocation RBraceLoc) { |
690 | auto *ED = cast<ExportDecl>(D); |
691 | if (RBraceLoc.isValid()) |
692 | ED->setRBraceLoc(RBraceLoc); |
693 | |
694 | PopDeclContext(); |
695 | |
696 | if (!D->isInvalidDecl()) { |
697 | SourceLocation BlockStart = |
698 | ED->hasBraces() ? ED->getBeginLoc() : SourceLocation(); |
699 | for (auto *Child : ED->decls()) { |
700 | if (checkExportedDecl(*this, Child, BlockStart)) { |
701 | |
702 | |
703 | diagExportedUnnamedDecl(*this, UnnamedDeclKind::Context, Child, |
704 | BlockStart); |
705 | } |
706 | } |
707 | } |
708 | |
709 | return D; |
710 | } |