aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/PPDirectives.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-07-08[win][clang] Do not inject static_assert macro definition (#147030)Mariya Podchishchaeva1-17/+0
In ms-compatibility mode we inject static_assert macro definition if assert macro is defined. This is done by 8da090381d567d0ec555840f6b2a651d2997e4b3 for the sake of better diagnosing, in particular to emit a compatibility warning when static_assert keyword is used without inclusion of <assert.h>. Unfortunately it doesn't do a good job in c99 mode adding that macro unexpectedly for the users, so this patch removes macro injection and the diagnostics. --------- Co-authored-by: Corentin Jabot <corentinjabot@gmail.com>
2025-06-26[clang][Preprocessor] Handle the first pp-token in EnterMainSourceFile (#145244)yronglin1-3/+0
Depends on [[clang][Preprocessor] Add peekNextPPToken, makes look ahead next token without side-effects](https://github.com/llvm/llvm-project/pull/143898). This PR fix the performance regression that introduced in https://github.com/llvm/llvm-project/pull/144233. The original PR(https://github.com/llvm/llvm-project/pull/144233) handle the first pp-token in the main source file in the macro definition/expansion and `Lexer::Lex`, but the lexer is almost always on the hot path, we may hit a performance regression. In this PR, we handle the first pp-token in `Preprocessor::EnterMainSourceFile`. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-06-26[NFC][Clang][Preprocessor] Refine the implementation of isNextPPTokenOneOf ↵yronglin1-2/+2
(#145546) This PR follow the suggestion(https://github.com/llvm/llvm-project/pull/143898#discussion_r2164253141) to refine the implementation of `Preprocessor::isNextPPToken`, also use C++ fold expression to refine `Token::isOneOf`. We don't need `bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const` anymore. In order to reduce the impact, specificed `TokenKind` is still passed to `Token::isOneOf` and `Preprocessor::isNextPPTokenOneOf` as function parameters. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-06-24[clang][Preprocessor] Add peekNextPPToken, makes look ahead next token ↵yronglin1-2/+2
without side-effects (#143898) This PR introduce a new function `peekNextPPToken`. It's an extension of `isNextPPTokenLParen` and can makes look ahead one token in preprocessor without side-effects. It's also the 1st part of https://github.com/llvm/llvm-project/pull/107168 and it was used to look ahead next token then determine whether current lexing pp directive is one of pp-import or pp-module directive. At the start of phase 4 an import or module token is treated as starting a directive and are converted to their respective keywords iff: - After skipping horizontal whitespace are - at the start of a logical line, or - preceded by an export at the start of the logical line. - Are followed by an identifier pp token (before macro expansion), or - <, ", or : (but not ::) pp tokens for import, or - ; for module Otherwise the token is treated as an identifier. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-06-21[C++][Modules] A module directive may only appear as the first preprocessing ↵yronglin1-0/+3
tokens in a file (#144233) This PR is 2nd part of [P1857R3](https://github.com/llvm/llvm-project/pull/107168) implementation, and mainly implement the restriction `A module directive may only appear as the first preprocessing tokens in a file (excluding the global module fragment.)`: [cpp.pre](https://eel.is/c++draft/cpp.pre): ``` module-file: pp-global-module-fragment[opt] pp-module group[opt] pp-private-module-fragment[opt] ``` We also refine tests use `split-file` instead of conditional macro. Signed-off-by: yronglin <yronglin777@gmail.com>
2025-05-29[clang][Lex][NFC] Reorder SrcMgr checks in CheckMacroName (#141483)Timm Baeder1-2/+4
isInPredefinedFile() will look at the presumed loc, which is comparatively slow. Move it after isInSystemFile(). http://llvm-compile-time-tracker.com/compare.php?from=843e362318e884991e517a54446b4faeacdad789&to=de0421a1a38052042721a67a6094f5cb38431f26&stat=instructions:u
2025-05-28[C2y] Add stdcountof.h (#140890)Aaron Ballman1-2/+2
WG14 N3469 changed _Lengthof to _Countof but it also introduced the <stdcountof.h> header to expose a macro with a non-ugly identifier. GCC vends this header as part of the compiler implementation, so Clang should do the same. Suggested-by: Alejandro Colomar <alx@kernel.org>
2025-05-16[clang] Use llvm::binary_search (NFC) (#140216)Kazu Hirata1-2/+1
2025-05-12[NFC] Optimize file kind determination (#139492)Serge Pavlov1-5/+3
There are checks in clang codebase that determine the type of source file, associated with a given location - specifically, if it is an ordonary file or comes from sources like command-line options or a built-in definitions. These checks often rely on calls to `getPresumedLoc`, which is relatively expensive. In certain cases, these checks are combined, leading to repeated calculations of the costly function negatively affecting compile time. This change tries to optimize such checks. It must fix compile time regression introduced in https://github.com/llvm/llvm-project/pull/137306/. --------- Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2025-05-04[clang] Remove unused local variables (NFC) (#138453)Kazu Hirata1-1/+0
2025-05-01[Clang] Do not warn for serialized builtin or command-line definitions (#137306)Juan Manuel Martinez Caamaño1-2/+6
When using `-dD` to generate a preprocessed output, the `#define` directives are preserved. This includes built-in and command-line definitions. Before, clang would warn for reserved identifiers for serialized built-in and command-line definitions. This patch adds an exception to these cases.
2025-04-17Reland [clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵yronglin1-11/+11
structures to `IdentifierLoc` (#136077) This PR reland https://github.com/llvm/llvm-project/pull/135808, fixed some missed changes in LLDB. I found this issue when I working on https://github.com/llvm/llvm-project/pull/107168. Currently we have many similiar data structures like: - std::pair<IdentifierInfo *, SourceLocation>. - Element type of ModuleIdPath. - IdentifierLocPair. - IdentifierLoc. This PR unify these data structures to IdentifierLoc, moved IdentifierLoc definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-04-17[clang] Implement StmtPrinter for EmbedExpr (#135957)Mariya Podchishchaeva1-2/+14
Tries to avoid memory leaks previously caused by saving filename by allocating memory in the preprocessor. Fixes https://github.com/llvm/llvm-project/issues/132641 Fixes https://github.com/llvm/llvm-project/issues/107869 --------- Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2025-04-16Revert "[clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵Michael Buch1-11/+11
structures to `IdentifierLoc`" (#135974) Reverts llvm/llvm-project#135808 Example from the LLDB macOS CI: https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/24084/execution/node/54/log/?consoleFull ``` /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp:360:49: error: no viable conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'clang::ModuleIdPath' (aka 'ArrayRef<IdentifierLoc>') clang::Module *top_level_module = DoGetModule(clang_path.front(), false); ^~~~~~~~~~~~~~~~~~ /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:41:40: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'const llvm::ArrayRef<clang::IdentifierLoc> &' for 1st argument class LLVM_GSL_POINTER [[nodiscard]] ArrayRef { ^ /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:41:40: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'llvm::ArrayRef<clang::IdentifierLoc> &&' for 1st argument /Users/ec2-user/jenkins/workspace/llvm.org/as-lldb-cmake/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:70:18: note: candidate constructor not viable: no known conversion from 'std::pair<clang::IdentifierInfo *, clang::SourceLocation>' to 'std::nullopt_t' for 1st argument /*implicit*/ ArrayRef(std::nullopt_t) {} ```
2025-04-16[clang] Unify `SourceLocation` and `IdentifierInfo*` pair-like data ↵yronglin1-11/+11
structures to `IdentifierLoc` (#135808) I found this issue when I working on https://github.com/llvm/llvm-project/pull/107168. Currently we have many similiar data structures like: - `std::pair<IdentifierInfo *, SourceLocation>`. - Element type of `ModuleIdPath`. - `IdentifierLocPair`. - `IdentifierLoc`. This PR unify these data structures to `IdentifierLoc`, moved `IdentifierLoc` definition to SourceLocation.h, and deleted other similer data structures. --------- Signed-off-by: yronglin <yronglin777@gmail.com>
2025-04-15[clang] Reject character devices in #embed for now (#135370)Mariya Podchishchaeva1-0/+6
See https://github.com/llvm/llvm-project/issues/126629 . Right now they are not supported properly and support requires modifying several layers of LLVM. For now simply reject them while proper support is being developed to avoid potential security problems.
2025-04-11[C23] Fix failed assertions with invalid #embed parameters (#135368)Aaron Ballman1-1/+3
If the invalid parameter was not the last parameter given, we would fail to skip to the end of the directive and trip a failed assertion. Fixes #126940
2025-04-10[clang][index] Skip over `#include UNDEF_IDENT` in single-file-parse mode ↵Jan Svoboda1-0/+9
(#135218) In the 'single-file-parse' mode, seeing `#include UNDEFINED_IDENTIFIER` should not be treated as an error. The identifier might be defined in a header that we decided to skip, resulting in a nonsensical diagnostic from the user point of view.
2025-04-04[clang] Do not share ownership of `PreprocessorOptions` (#133467)Jan Svoboda1-10/+10
This PR makes it so that `CompilerInvocation` is the sole owner of the `PreprocessorOptions` instance.
2025-01-23[Clang] restrict use of attribute names reserved by the C++ standard (#106036)Oleksandr T.1-1/+27
Fixes #92196 https://eel.is/c++draft/macro.names#2 > A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table [4](https://eel.is/c++draft/lex.name#tab:lex.name.special), or to the [attribute-token](https://eel.is/c++draft/dcl.attr.grammar#nt:attribute-token)s described in [[dcl.attr]](https://eel.is/c++draft/dcl.attr), except that the names likely and unlikely may be defined as function-like macros ([[cpp.replace]](https://eel.is/c++draft/cpp.replace))[.](https://eel.is/c++draft/macro.names#2.sentence-1)
2024-11-16[Lex] Remove unused includes (NFC) (#116460)Kazu Hirata1-4/+0
Identified with misc-include-cleaner.
2024-10-21[SystemZ][z/OS] Add new openFileForReadBinary function, and pass IsText ↵Abhina Sree1-8/+8
parameter to getBufferForFile (#111723) This patch adds an IsText parameter to the following getBufferForFile, getBufferForFileImpl. We introduce a new virtual function openFileForReadBinary which defaults to openFileForRead except in RealFileSystem which uses the OF_None flag instead of OF_Text. The default is set to OF_Text instead of OF_None, this change in value does not affect any other platforms other than z/OS. Setting this parameter correctly is required to open files on z/OS in the correct encoding. The IsText parameter is based on the context of where we open files, for example, in the ASTReader, HeaderMap requires that files always be opened in binary even though they might be tagged as text.
2024-07-19[clang] Fix static analyzer concerns in #embed code (#99331)Mariya Podchishchaeva1-5/+3
1. Dead code in `LookupEmbedFile`. The loop always exited on the first iteration. This was also causing a bug of not checking all directories provided by `--embed-dir`. 2. Use of uninitialized variable `CurTok` in `LexEmbedParameters`. It was used to initialize the field which seems to be unused. Removed unused field, this way `CurTok` should be initialized by Lex method.
2024-06-22[clang] Fix -Wsign-compare in 32-bit buildsFangrui Song1-1/+1
2024-06-20Reland [clang][Sema, Lex, Parse] Preprocessor embed in C and C++ (#95802)Mariya Podchishchaeva1-2/+473
This commit implements the entirety of the now-accepted [N3017 -Preprocessor Embed](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm) and its sister C++ paper [p1967](https://wg21.link/p1967). It implements everything in the specification, and includes an implementation that drastically improves the time it takes to embed data in specific scenarios (the initialization of character type arrays). The mechanisms used to do this are used under the "as-if" rule, and in general when the system cannot detect it is initializing an array object in a variable declaration, will generate EmbedExpr AST node which will be expanded by AST consumers (CodeGen or constant expression evaluators) or expand embed directive as a comma expression. This reverts commit https://github.com/llvm/llvm-project/commit/682d461d5a231cee54d65910e6341769419a67d7. --------- Co-authored-by: The Phantom Derpstorm <phdofthehouse@gmail.com> Co-authored-by: Aaron Ballman <aaron@aaronballman.com> Co-authored-by: cor3ntin <corentinjabot@gmail.com> Co-authored-by: H. Vetinari <h.vetinari@gmx.com>
2024-06-12Revert "✨ [Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C ↵Vitaly Buka1-475/+2
and Obj-C++ by-proxy)" (#95299) Reverts llvm/llvm-project#68620 Introduce or expose a memory leak and UB, see llvm/llvm-project#68620
2024-06-12[clang][Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C and ↵The Phantom Derpstorm1-2/+475
Obj-C++ by-proxy) (#68620) This commit implements the entirety of the now-accepted [N3017 - Preprocessor Embed](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm) and its sister C++ paper [p1967](https://wg21.link/p1967). It implements everything in the specification, and includes an implementation that drastically improves the time it takes to embed data in specific scenarios (the initialization of character type arrays). The mechanisms used to do this are used under the "as-if" rule, and in general when the system cannot detect it is initializing an array object in a variable declaration, will generate EmbedExpr AST node which will be expanded by AST consumers (CodeGen or constant expression evaluators) or expand embed directive as a comma expression. --------- Co-authored-by: Aaron Ballman <aaron@aaronballman.com> Co-authored-by: cor3ntin <corentinjabot@gmail.com> Co-authored-by: H. Vetinari <h.vetinari@gmx.com> Co-authored-by: Podchishchaeva, Mariya <mariya.podchishchaeva@intel.com>
2024-05-11[clang] Use StringRef::operator== instead of StringRef::equals (NFC) (#91844)Kazu Hirata1-2/+2
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 24 under clang/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-05[clang][Modules] Make `Module::Requirement` a struct (NFC) (#67900)David Stone1-1/+2
`Module::Requirement` was defined as a `std::pair<std::string, bool>`. This required a comment to explain what the data members mean and makes the usage harder to understand. Replace this with a struct with two members, `FeatureName` and `RequiredState`. --------- Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2024-02-08[clang][lex] Fix build failure after da95d926Jan Svoboda1-4/+4
2024-02-08[clang][lex] Always pass suggested module to `InclusionDirective()` callback ↵Jan Svoboda1-35/+35
(#81061) This patch provides more information to the `PPCallbacks::InclusionDirective()` hook. We now always pass the suggested module, regardless of whether it was actually imported or not. The extra `bool ModuleImported` parameter then denotes whether the header `#include` will be automatically translated into import the the module. The main change is in `clang/lib/Lex/PPDirectives.cpp`, where we take care to not modify `SuggestedModule` after it's been populated by `LookupHeaderIncludeOrImport()`. We now exclusively use the `SM` (`ModuleToImport`) variable instead, which has been equivalent to `SuggestedModule` until now. This allows us to use the original non-modified `SuggestedModule` for the callback itself. (This patch turns out to be necessary for https://github.com/apple/llvm-project/pull/8011).
2024-02-06[CLANG] Fix INF/NAN warning. (#80290)Zahira Ammarguellat1-1/+1
In https://github.com/llvm/llvm-project/pull/76873 a warning was added when the macros INFINITY and NAN are used in binary expressions when -menable-no-nans or -menable-no-infs are used. If the user uses an option that nullifies these two options, the warning will still be generated. This patch adds an additional information to the warning comment to let the user know about this. It also suppresses the warning when #ifdef INFINITY, #ifdef NAN, #ifdef NAN or #ifndef NAN are used in the code.
2023-12-18[clang][lex] Fix non-portability diagnostics with absolute path (#74782)Jan Svoboda1-7/+15
The existing code incorrectly assumes that `Path` can be empty. It can't, it always contains at least `<` or `"`. On Unix, this patch fixes an incorrect diagnostics that instead of `"/Users/blah"` suggested `"Userss/blah"`. In assert builds, this would outright crash. This patch also fixes a bug on Windows that would prevent the diagnostic being triggered due to separator mismatch. rdar://91172342
2023-12-13[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)Kazu Hirata1-6/+6
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
2023-12-08[clang] NFC: Remove `OptionalFileEntryRefDegradesToFileEntryPtr` (#74899)Jan Svoboda1-1/+2
2023-11-06[clang][modules] Avoid modules diagnostics for `__has_include()` (#71450)Jan Svoboda1-16/+20
After #70144 Clang started resolving module maps even for `__has_include()` expressions. This had the unintended consequence of emitting diagnostics around header misuse. These don't make sense if you actually don't bring contents of the header into the importer, so should be skipped for `__has_include()`. This patch moves emission of these diagnostics out of `Preprocessor::LookupFile()` up into `Preprocessor::LookupHeaderIncludeOrImport()`.
2023-11-02[C++20] [Modules] Warn if we found #include <filename> in module purview ↵Chuanqi Xu1-0/+4
(#69555) Close https://github.com/llvm/llvm-project/issues/68615. It is generally wrong to include <filename> in the module purview. Although there are cases to include files in the module purview, generally these use cases should include files by quotes instead of by angles. Here we think the files got included by angles are the system headers. This is consistency with MSVC too: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warnings-by-compiler-version?view=msvc-170#warnings-introduced-in-visual-studio-2022-version-170-compiler-version-1930
2023-10-27[clang] use relative paths for builtin headers during module compilation ↵Richard Howell1-1/+8
(#68023) When including builtin headers as part of a system module, ensure we use relative paths to those headers. Otherwise the module will fail to compile when specifying relative resource directories without extra search paths.
2023-10-17[clang] Implement C23 <stdckdint.h>zijunzhao1-3/+4
https://github.com/llvm/llvm-project/issues/62248 Reviewed By: yabinc, aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D157331
2023-10-08[clang][Modules] `checkModuleIsAvailable` should use a const & parameter ↵David Stone1-8/+10
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 Svoboda1-4/+3
This is the `ASTReader` counterpart to PR #67383.
2023-09-28[clang][modules] Use `FileEntryRef` in `ModuleMap` (2/2)Jan Svoboda1-2/+2
2023-09-09[clang] NFCI: Use `FileEntryRef` in `LookupSubframeworkHeader()`Jan Svoboda1-3/+3
2023-09-09[clang] NFCI: Use `FileEntryRef` in `suggestPathToFileForDiagnostics()`Jan Svoboda1-4/+4
2023-09-05[NFC][Clang] Fix static code analyzer concern about null value dereferenceManna, Soumi1-1/+3
CurLexer is dereferenced and should not be null in clang::Preprocessor::SkipExcludedConditionalBlock(clang::SourceLocation, clang::SourceLocation, bool, bool, clang::SourceLocation) This patch adds an assert for NULL value check of pointer CurLexer and splits up all predicates so that, when/if a failure occurs, we'll be able to tell which predicate failed. Reviewed By: tahonermann Differential Revision: https://reviews.llvm.org/D158293
2023-08-11[C23] Rename C2x->C23 in diagnosticsAaron Ballman1-5/+5
This renames C2x to C23 in diagnostic identifiers and messages. The changes were made mechanically.
2023-08-11[C23] Rename C2x -> C23; NFCAaron Ballman1-7/+7
This does the rename for most internal uses of C2x, but does not rename or reword diagnostics (those will be done in a follow-up). I also updated standards references and citations to the final wording in the standard.
2023-06-01[Lex] Only warn on defining or undefining language-defined builtinsJohn Brawn1-8/+27
D144654 made it so that we warn on any defining or undefining of builtin macros. However the C and C++ standards only forbid the defining or undefining of macros defined in the language standard itself, but clang defines more macros than those and warning on those may not be helpful. Resolve this by only warning if the builtin macro name is the name of a macro defined by the language. This is done in a way that removes some of the existing checks, as those were made redundant by restricting the warning in this way. Differential Revision: https://reviews.llvm.org/D151741
2023-05-31[clang] NFCI: Use `FileEntryRef` in `PPDirectives`Jan Svoboda1-7/+7
This is a prep patch that enables removal of some calls to the deprecated `{File,Directory}Entry::getName()`.
2023-05-30[clang][lex] NFCI: Use DirectoryEntryRef in HeaderSearch::LookupFileJan Svoboda1-8/+9
This patch changes the argument type to `HeaderSearch::LookupFile()` from `const DirectoryEntry *` to `DirectoryEntryRef` in order to remove some calls to the deprecated `DirectoryEntry::getName()`. Depends on D127660. Reviewed By: bnbarham, benlangmuir Differential Revision: https://reviews.llvm.org/D127663