summaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/FrontendAction.cpp
AgeCommit message (Collapse)Author
2025-11-21[clang][NFC] Inline Frontend/FrontendDiagnostic.h -> ↵Jordan Rupprecht
Basic/DiagnosticFrontend.h (#162883) d076608d58d1ec55016eb747a995511e3a3f72aa moved some deps around to avoid cycles and left clang/Frontend/FrontendDiagnostic.h as a shim that simply includes clang/Basic/DiagnosticFrontend.h. This PR inlines it so that nothing in tree still includes clang/Frontend/FrontendDiagnostic.h. Doing this will help prevent future layering issues. See #162865. Frontend already depends on Basic, so no new deps need to be added anywhere except for places that do strict dep checking.
2025-10-22[clang] Don't silently inherit the VFS from `FileManager` (#164323)Jan Svoboda
Since https://github.com/llvm/llvm-project/pull/158381 the `CompilerInstance` is aware of the VFS and co-owns it. To reduce scope of that PR, the VFS was being inherited from the `FileManager` during `setFileManager()` if it wasn't configured before. However, the implementation of that setter was buggy. This PR fixes the bug, and moves us closer to the long-term goal of `CompilerInstance` requiring the VFS to be configured explicitly and owned by the instance.
2025-10-20[ADT] Prepare for deprecation of StringSwitch cases with 4+ args. NFC. (#164173)Jakub Kuderski
Update `.Cases` and `.CasesLower` with 4+ args to use the `initializer_list` overload. The deprecation of these functions will come in a separate PR. For more context, see: https://github.com/llvm/llvm-project/pull/163405.
2025-10-02[clang] NFCI: Clean up `CompilerInstance::create{File,Source}Manager()` ↵Jan Svoboda
(#160748) The `CompilerInstance::createSourceManager()` function currently accepts the `FileManager` to be used. However, all clients call `CompilerInstance::createFileManager()` prior to creating the `SourceManager`, and it never makes sense to use a `FileManager` in the `SourceManager` that's different from the rest of the compiler. Passing the `FileManager` explicitly is redundant, error-prone, and deviates from the style of other `CompilerInstance` initialization APIs. This PR therefore removes the `FileManager` parameter from `createSourceManager()` and also stops returning the `FileManager` pointer from `createFileManager()`, since that was its primary use. Now, `createSourceManager()` internally calls `getFileManager()` instead.
2025-09-18[clang] Pass VFS into `ASTUnit::LoadFromASTFile()` (#159166)Jan Svoboda
This PR makes the `VFS` parameter to `ASTUnit::LoadFromASTFile()` required and explicit, rather than silently defaulting to the real file system. This makes it easy to correctly propagate the fully-configured VFS and load any input files like the rest of the compiler does.
2025-09-16[clang] Initialize the file system explicitly (#158381)Jan Svoboda
This PR is a part of the effort to make the VFS used in the compiler more explicit and consistent. Instead of creating the VFS deep within the compiler (in `CompilerInstance::createFileManager()`), clients are now required to explicitly call `CompilerInstance::createVirtualFileSystem()` and provide the base VFS from the outside. This PR also helps in breaking up the dependency cycle where creating a properly configured `DiagnosticsEngine` requires a properly configured VFS, but creating properly configuring a VFS requires the `DiagnosticsEngine`. Both `CompilerInstance::create{FileManager,Diagnostics}()` now just use the VFS already in `CompilerInstance` instead of taking one as a parameter, making the VFS consistent across the instance sub-object.
2025-09-02[clang] load umbrella dir headers in sorted order (#156108)Richard Howell
Clang modules sort the umbrella dir headers by name before adding to the module's includes to ensure deterministic output across different file systems. This is insufficient however, as the header search table is also serialized. This includes all the loaded headers by file reference, which are allocated incrementally. To ensure stable output we have to also create the file references in sorted order.
2025-08-04[Clang] Handle C++20 export declarations in -dump-minimization-hints (#151666)Ilya Biryukov
Thanks to Justin Stitt for bringing this up, providing test cases and a direction for the fix!
2025-08-01NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151782)James Y Knight
This commit handles the following types: - clang::ExternalASTSource - clang::TargetInfo - clang::ASTContext - clang::SourceManager - clang::FileManager Part of cleanup #151026
2025-08-01[Clang] Dump minimization hints for namespaces (#151534)Ilya Biryukov
Unlike other declarations, these cover two ranges: - from `namespace/inline namespace` to the opening `{`, - the closing `}`. This allows to mark the declarations inside the namespace itself independently.
2025-08-01[Clang] Flush minimization hints after writing (#151522)Ilya Biryukov
This helps to get well-formed output if Clang crashes after that point.
2025-07-31NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151545)James Y Knight
Handles clang::DiagnosticsEngine and clang::DiagnosticIDs. For DiagnosticIDs, this mostly migrates from `new DiagnosticIDs` to convenience method `DiagnosticIDs::create()`. Part of cleanup https://github.com/llvm/llvm-project/issues/151026
2025-07-15[clang][modules] Serialize `CodeGenOptions` (#146422)Jan Svoboda
Some `LangOptions` duplicate their `CodeGenOptions` counterparts. My understanding is that this was done solely because some infrastructure (like preprocessor initialization, serialization, module compatibility checks, etc.) were only possible/convenient for `LangOptions`. This PR implements the missing support for `CodeGenOptions`, which makes it possible to remove some duplicate `LangOptions` fields and simplify the logic. Motivated by https://github.com/llvm/llvm-project/pull/146342.
2025-07-04[clang-tidy] EndSourceFile() for preprocessor before diagnostic client (#145784)Dave Bartolomeo
The comment for `DiagnosticConsumer::BeginSourceFile()` states that "diagnostics with source range information are required to only be emitted in between BeginSourceFile() and EndSourceFile().". While working on some upcoming changes to the static analyzer, we hit some crashes when diagnostics were reported from the `EndOfMainFile` callback in the preprocessor. This turned out to be because `FrontEndAction::EndSourceFile()` notifies the diagnostic clients of the end of the source file before it notifies the preprocessor. Thus, the diagnostics from the preprocessor callback are reported when the diagnostic client is no longer expecting any diagnostics. The fix is to swap the order of the `EndSourceFile()` calls so that the preprocessor is notified first. I've added asserts to the `ClangTidyDiagnosticConsumer` to catch unexpected diagnostics outside of a source file. Before swapping the order of the calls as described above, this causes several failures in the clang-tidy regression tests. With the swap, there are no failures in `check-all`. rdar://141230583
2025-06-05[Clang][NFC] Use move in std::vector local in HandleTranslationUnit (#142851)Shafik Yaghmour
Static analysis flagged this since we could move MergedRanges since it is a std::vector, a local and unused after that line. So there is a potential saving.
2025-05-22Reapply "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Jan Svoboda
(#139584)" This reverts commit e2a885537f11f8d9ced1c80c2c90069ab5adeb1d. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22Revert "[clang] Remove intrusive reference count from `DiagnosticOptions` ↵Kazu Hirata
(#139584)" This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c. Multiple builtbot failures have been reported: https://github.com/llvm/llvm-project/pull/139584
2025-05-22[clang] Remove intrusive reference count from `DiagnosticOptions` (#139584)Jan Svoboda
The `DiagnosticOptions` class is currently intrusively reference-counted, which makes reasoning about its lifetime very difficult in some cases. For example, `CompilerInvocation` owns the `DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and only exposes an accessor returning `DiagnosticOptions &`. One would think this gives `CompilerInvocation` exclusive ownership of the object, but that's not the case: ```c++ void shareOwnership(CompilerInvocation &CI) { llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions(); // ... } ``` This is a perfectly valid pattern that is being actually used in the codebase. I would like to ensure the ownership of `DiagnosticOptions` by `CompilerInvocation` is guaranteed to be exclusive. This can be leveraged for a copy-on-write optimization later on. This PR changes usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and `lldb` to not be intrusively reference-counted.
2025-05-11[clang] Use std::tie to implement operator< (NFC) (#139438)Kazu Hirata
2025-05-06[clang][modules] Lazily load by name lookups in module maps (#132853)Michael Spencer
Instead of eagerly populating the `clang::ModuleMap` when looking up a module by name, this patch changes `HeaderSearch` to only load the modules that are actually used. This introduces `ModuleMap::findOrLoadModule` which will load modules from parsed but not loaded module maps. This cannot be used anywhere that the module loading code calls into as it can create infinite recursion. This currently just reparses module maps when looking up a module by header. This is fine as redeclarations are allowed from the same file, but future patches will also make looking up a module by header lazy. This patch changes the shadow.m test to use explicitly built modules and `#import`. This test and the shadow feature are very brittle and do not work in general. The test relied on pcm files being left behind by prior failing clang invocations that were then reused by the last invocation. If you clean the cache then the last invocation will always fail. This is because the input module map and the `-fmodule-map-file=` module map are parsed in the same module scope, and `-fmodule-map-file=` is forwarded to implicit module builds. That means you are guaranteed to hit a module redeclaration error if the TU actually imports the module it is trying to shadow. This patch changes when we load A2's module map to after the `A` module has been loaded, which sets the `IsFromModuleFile` bit on `A`. This means that A2's `A` is skipped entirely instead of creating a shadow module, and we get textual inclusion. It is possible to construct a case where this would happen before this patch too. An upcoming patch in this series will rework shadowing to work in the general case, but that's only possible once header -> module lookup is lazy too.
2025-04-29[clang] Hide the `LangOptions` pointer from `CompilerInvocation` (#137675)Jan Svoboda
This PR makes `CompilerInvocation` the sole owner of the `LangOptions` instance.
2025-04-25[clang] Do not share ownership of `HeaderSearchOptions` (#132984)Jan Svoboda
This PR makes it so that `CompilerInvocation` is the sole owner of the `HeaderSearchOptions` instance.
2025-04-17Mark the file opened by DeserializedDeclsSourceRangePrinter as a text file ↵Zibi Sarbinowski
(#135842) This PR will fix the following lit failure seeing on z/OS and most likely on Windows: `FAIL: Clang :: Frontend/dump-minimization-hints.cpp` Without `OF_TextWithCRLF` flag, a file is treated as binary and is read improperly, at least on z/OS.
2025-04-11Implement `-dump-minimization-hints` flag. (#133910)Viktoriia Bakalova
This PR implements a CC1 flag `-dump-minimization-hints`. The flag allows to specify a file path to dump ranges of deserialized declarations in `ASTReader`. Example usage: ``` clang -Xclang=-dump-minimization-hints=/tmp/decls -c file.cc -o file.o ``` Example output: ``` // /tmp/decls { "required_ranges": [ { "file": "foo.h", "range": [ { "from": { "line": 26, "column": 1 }, "to": { "line": 27, "column": 77 } } ] }, { "file": "bar.h", "range": [ { "from": { "line": 30, "column": 1 }, "to": { "line": 35, "column": 1 } }, { "from": { "line": 92, "column": 1 }, "to": { "line": 95, "column": 1 } } ] } ] } ``` Specifying the flag creates an instance of `DeserializedDeclsSourceRangePrinter`, which dumps ranges of deserialized declarations to aid debugging and bug minimization (we use is as input to [C-Vise](https://github.com/emaxx-google/cvise/tree/multifile-hints). Required ranges are computed from source ranges of Decls. `TranslationUnitDecl`, `LinkageSpecDecl` and `NamespaceDecl` are ignored for the sake of this PR. Technical details: * `DeserializedDeclsSourceRangePrinter` implements `ASTConsumer` and `ASTDeserializationListener`, so that an object of `DeserializedDeclsSourceRangePrinter` registers as its own listener. * `ASTDeserializationListener` interface provides the `DeclRead` callback that we use to collect the deserialized Decls. Printing or otherwise processing them as this point is dangerous, since that could trigger additional deserialization and crash compilation. * The collected Decls are processed in `HandleTranslationUnit` method of `ASTConsumer`. This is a safe point, since we know that by this point all the Decls needed by the compiler frontend have been deserialized. * In case our processing causes further deserialization, `DeclRead` from the listener might be called again. However, at that point we don't accept any more Decls for processing.
2025-03-29[Clang] Check PP presence when printing stats (#131608)Qiu Chaofan
Front-end option `-print-stats` can be used to print statistics around the compilation process. But clang with this options will crash when input is IR file. This patch fixes the crash by checking preprocessor presence before invoking it.
2025-03-28[clang] Implement some missing interfaces for ↵Haojian Wu
DelegatingDeserializationListener (#133424) Split from the https://github.com/llvm/llvm-project/pull/133395 per the review comment. This patch also moves the `DelegatingDeserializationListener` close to `ASTDeserializationListener`.
2025-01-12-ftime-report: Move FrontendTimer closer to TimeTraceScopeFangrui Song
... to improve consistency and make "Clang time report" cover `FrontendAction::BeginSourceFile` and `FrontendAction::EndSourceFile`.
2024-10-28[clang][modules] Preserve the module map that allowed inferring (#113389)Jan Svoboda
With inferred modules, the dependency scanner takes care to replace the fake "__inferred_module.map" path with the file that allowed the module to be inferred. However, this only worked when such a module was imported directly in the TU. Whenever such module got loaded transitively, the scanner would fail to perform the replacement. This is caused by the fact that PCM files are lossy and drop this information. This patch makes sure that PCMs include this file for each submodule (in the `SUBMODULE_DEFINITION` record), fixes one existing test with an incorrect assertion, and does a little drive-by refactoring of `ModuleMap`.
2024-10-25[clang][modules] Shrink the size of `Module::Headers` (#113395)Jan Svoboda
This patch shrinks the size of the `Module` class from 2112B to 1624B. I wasn't able to get a good data on the actual impact on memory usage, but given my `clang-scan-deps` workload at hand (with tens of thousands of instances), I think there should be some win here. This also speeds up my benchmark by under 0.1%.
2024-09-23[Frontend] Teach LoadFromASTFile to take FileName by StringRef (NFC) (#109583)Kazu Hirata
Without this patch, several callers of LoadFromASTFile construct an instance of std::string to be passed as FileName, only to be converted back to StringRef when LoadFromASTFile calls ReadAST. This patch changes the type of FileName to StringRef and updates the callers.
2024-05-13[NFC] [Serialization] Merge IdentID with IdentifierIDChuanqi Xu
In ASTBitCodes.h, there are two type alias for the ID type of Identifiers with the same underlying type. It is confusing. This patch tries to merge the `IdentID` to `IdentifierID` to erase such confusion.
2024-04-25[Clang] Diagnose apply AST consume actions on LLVM IR (#88602)yronglin
Fixes https://github.com/llvm/llvm-project/issues/88522 This PR introduce a new diagnostic to report apply AST consume actions on LLVM IR. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2024-04-25[NFC] [Serialization] Avoid using DeclID directly as much as possibleChuanqi Xu
This patch tries to remove all the direct use of DeclID except the real low level reading and writing. All the use of DeclID is converted to the use of LocalDeclID or GlobalDeclID. This is helpful to increase the readability and type safety.
2024-04-25Revert "[NFC] [Serialization] Avoid using DeclID directly as much as possible"Chuanqi Xu
This reverts commit 42070a5c092ed420bf92ebf38229c594885e94c7. I forgot to touch lldb.
2024-04-25[NFC] [Serialization] Avoid using DeclID directly as much as possibleChuanqi Xu
This patch tries to remove all the direct use of DeclID except the real low level reading and writing. All the use of DeclID is converted to the use of LocalDeclID or GlobalDeclID. This is helpful to increase the readability and type safety.
2024-04-25[NFC] Move DeclID from serialization/ASTBitCodes.h to AST/DeclID.h (#89873)Chuanqi Xu
Previously, the DeclID is defined in serialization/ASTBitCodes.h under clang::serialization namespace. However, actually the DeclID is not purely used in serialization part. The DeclID is already widely used in AST and all around the clang project via classes like `LazyPtrDecl` or calling `ExternalASTSource::getExernalDecl()`. All such uses are via the raw underlying type of `DeclID` as `uint32_t`. This is not pretty good. This patch moves the DeclID class family to a new header `AST/DeclID.h` so that the whole project can use the wrapped class `DeclID`, `GlobalDeclID` and `LocalDeclID` instead of the raw underlying type. This can improve the readability and the type safety.
2024-03-28[clang][modules] Avoid calling expensive `SourceManager::translateFile()` ↵Jan Svoboda
(#86216) The `ASTWriter` algorithm for computing affecting module maps uses `SourceManager::translateFile()` to get a `FileID` from a `FileEntry`. This is slow (O(n)) since the function performs a linear walk over `SLocEntries` until it finds one with a matching `FileEntry`. This patch removes this use of `SourceManager::translateFile()` by tracking `FileID` instead of `FileEntry` in couple of places in `ModuleMap`, giving `ASTWriter` the desired `FileID` directly. There are no changes required for clients that still want a `FileEntry` from `ModuleMap`: the existing APIs internally use `SourceManager` to perform the reverse `FileID` to `FileEntry` conversion in O(1).
2024-02-23[C++20] [Modules] Allow to compile a pcm with and without -fPICChuanqi Xu
seperately We can compile a module unit in 2 phase compilaton: ``` clang++ -std=c++20 a.cppm --precompile -o a.pcm clang++ -std=c++20 a.pcm -c -o a.o ``` And it is a general requirement that we need to compile a translation unit with and without -fPIC for static and shared libraries. But for C++20 modules with 2 phase compilation, it may be waste of time to compile them 2 times completely. It may be fine to generate one BMI and compile it with and without -fPIC seperately. e.g., ``` clang++ -std=c++20 a.cppm --precompile -o a.pcm clang++ -std=c++20 a.pcm -c -o a.o clang++ -std=c++20 a.pcm -c -fPIC -o a-PIC.o ``` Then we can save the time to parse a.cppm repeatedly.
2023-11-28[clang] Remove unused argument. NFC. (#73594)Juergen Ributzka
2023-10-08[clang][Modules] `checkModuleIsAvailable` should use a const & parameter ↵David Stone
instead of pointer (#67902) The `Module` parameter to `checkModuleIsAvailable` is currently passed by pointer to non-const. However, it requires only const access and it cannot be null. Change this to be a reference to const instead. This then makes it obvious that it is an input-only parameter, so move it to be before the in-out parameter for diagnostics.
2023-09-29[clang] NFCI: Use `FileEntryRef` in `ASTReader::GetHeaderFileInfo()`Jan Svoboda
This is the `ASTReader` counterpart to PR #67383.
2023-09-28[clang] NFCI: Use `FileEntryRef` in `SourceManager::setFileIsTransient()`Jan Svoboda
2023-09-13[clang] NFCI: Use `FileEntryRef` in `ModuleMap::InferredModuleAllowedBy`Jan Svoboda
2023-09-08Reapply "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda
This reapplies ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f, except for a tiny part that was reverted separately: 65331da0032ab4253a4bc0ddcb2da67664bd86a9. That will be reapplied later on, since it turned out to be more involved. This commit is enabled by 5523fefb01c282c4cbcaf6314a9aaf658c6c145f and f0f548a65a215c450d956dbcedb03656449705b9, specifically the part that makes 'clang-tidy/checkers/misc/header-include-cycle.cpp' separator agnostic.
2023-09-07[NFC] [C++20] [Modules] Refactor the warning to '-fmodule-file=<BMIPath>' ↵Chuanqi Xu
for C++20 modules Previous implementation of the warning to use `-fmodule-file=<BMIPath>` for C++20 Modules is not straightforward and it is problematic in case we read the BMIPath by writing tools based clang components. This patch refactors it with a simple and direct style.
2023-09-06Revert "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda
This reverts commit ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f. The 'clang-tidy/checkers/misc/header-include-cycle.cpp' test started failing on Windows: https://lab.llvm.org/buildbot/#/builders/216/builds/26855.
2023-09-06[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`Jan Svoboda
This commit replaces some calls to the deprecated `FileEntry::getName()` with `FileEntryRef::getName()` by swapping current usages of `SourceManager::getFileEntryForID()` with `SourceManager::getFileEntryRefForID()`. This lowers the number of usages of the deprecated `FileEntry::getName()` from 95 to 50.
2023-09-02[clang][clangd] Ensure the stack bottom before building ASTYounan Zhang
`clang::runWithSufficientStackSpace` requires the address of the initial stack bottom to prevent potential stack overflows. In addition, add a fallback to ASTFrontendAction in case any client forgets to call it when not through CompilerInstance::ExecuteAction, which is rare. Fixes https://github.com/clangd/clangd/issues/1745. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D158967
2023-07-28[NFC][clang] Fix static analyzer concernsPodchishchaeva, Mariya
DelegatingDeserializationListener frees resources in the destructor but doesn't have user-written copy c'tor or assignment operator, so copying it using default ones can cause double free. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D156405
2023-06-15[clang][index] NFCI: Make `CXFile` a `FileEntryRef`Jan Svoboda
This patch swaps out the `void *` behind `CXFile` from `FileEntry *` to `FileEntryRef::MapEntry *`. This allows us to remove some deprecated uses of `FileEntry::getName()`. Depends on D151854. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D151938