aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Parser/preprocessor.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-29[flang] Support #if defined when no definitions (#156080)Peter Klausler1-1/+1
The "defined" operator isn't working in #if/#elif expressions when there are no defined macros. Fix.
2025-08-27[flang][warnings] systematically guard warnings (#154234)Andre Kuhlenschmidt1-33/+18
This change modifies the messages API to make it impossible to forget to call ShouldWarn by moving the call inside of the API. The low level API should be avoided and developers should call Warn on a SemanticContext or FoldingContext.
2025-07-14[flang] Don't create bogus tokens from token pasting (##) (#147596)Peter Klausler1-8/+35
When blank tokens arise from macro replacement in token sequences with token pasting (##), the preprocessor is producing some bogus tokens (e.g., "name(") that can lead to subtle bugs later when macro names are not recognized as such. The fix is to not paste tokens together when the result would not be a valid Fortran or C token in the preprocessing context.
2025-07-03[flang][preprocessor] fix use of bitwise-and for logical-and (#146758)Andre Kuhlenschmidt1-2/+6
The preprocessor used bitwise and to implement logical, this is a bug. towards #146362
2025-05-30[flang] Add __COUNTER__ preprocessor macro (#136827)Yussur Mustafa Oraji1-0/+4
This commit adds support for the `__COUNTER__` preprocessor macro, which works the same as the one found in clang. It is useful to generate unique names at compile-time.
2025-05-15[flang] Support -D for function-like macros (#139812)Peter Klausler1-1/+75
Handle a command-line function-like macro definition like "-Dfoo(a)=...". TODO: error reporting for badly formed argument lists.
2025-05-01[flang] Fix #else with trailing text (#138045)Eugene Epshteyn1-1/+2
Fixed the issue, where the extra text on #else line (' Z' in the example below) caused the data from the "else" clause to be processed together with the data of "then" clause. ``` #ifndef XYZ42 PARAMETER(A=2) #else Z PARAMETER(A=3) #endif end ```
2025-04-18[flang] Don't perform macro replacement on exponents (#136176)Peter Klausler1-19/+19
See new test. I inadvertently broke this behavior with a recent fix for another problem, because the effects of the overloaded TokenSequence::Put() member function on token merging were confusing. Rename and document the various overloads.
2025-03-10[flang] Ignore empty keyword macros before directives (#130333)Peter Klausler1-0/+9
Ignore any keyword macros with empty directives that might appear before a compiler directive. Fixes https://github.com/llvm/llvm-project/issues/126459.
2024-11-05[flang] Handle "defined" in macro expansions (#114844)Peter Klausler1-55/+61
The preprocessor implements "defined(X)" and "defined X" in if/elif directive expressions in such a way that they only work at the top level, not when they appear in macro expansions. Fix that, which is a little tricky due to the need to detect the "defined" keyword before applying any macro expansion to its argument, and add a bunch of tests. Fixes https://github.com/llvm/llvm-project/issues/114064.
2024-10-02[flang] Tag warnings with LanguageFeature or UsageWarning (#110304)Peter Klausler1-6/+12
(This is a big patch, but it's nearly an NFC. No test results have changed and all Fortran tests in the LLVM test suites work as expected.) Allow a parser::Message for a warning to be marked with the common::LanguageFeature or common::UsageWarning that controls it. This will allow a later patch to add hooks whereby a driver will be able to decorate warning messages with the names of its options that enable each particular warning, and to add hooks whereby a driver can map those enumerators by name to command-line options that enable/disable the language feature and enable/disable the messages. The default settings in the constructor for LanguageFeatureControl were moved from its header file into its C++ source file. Hooks for a driver to use to map the name of a feature or warning to its enumerator were also added. To simplify the tagging of warnings with their corresponding language feature or usage warning, to ensure that they are properly controlled by ShouldWarn(), and to ensure that warnings never issue at code sites in module files, two new Warn() member function templates were added to SemanticsContext and other contextual frameworks. Warn() can't be used before source locations can be mapped to scopes, but the bulk of existing code blocks testing ShouldWarn() and FindModuleFile() before calling Say() were convertible into calls to Warn(). The ones that were not convertible were extended with explicit calls to Message::set_languageFeature() and set_usageWarning().
2024-09-12[flang][preprocessor] Change handling of macros in text from Fortran … ↵Peter Klausler1-1/+1
(#108113) …INCLUDE lines The compiler current treats an INCLUDE line as essentially a synonym for a preprocessing #include directive. The causes macros that have been defined at the point where the INCLUDE line is processed to be replaced within the text of the included file. This behavior is surprising to users who expect an INCLUDE line to be expanded into its contents *after* preprocessing has been applied to the original source file, with no further macro expansion. Change INCLUDE line processing to use a fresh instance of Preprocessor containing no macro definitions except _CUDA in CUDA Fortran compilations and, if the original file was being preprocessed, the standard definitions of __FILE__, __LINE__, and so forth.
2024-07-11[flang][preprocessor] Support __TIMESTAMP__ (#98057)Peter Klausler1-0/+15
Support the predefined macro __TIMESTAMP__ as interpreted by GCC. It expands to a character literal with the time of last modification of the top-level source file in asctime(3) format, e.g. "Tue Jul 4 10:18:05 1776".
2024-06-03[flang][preprocessing] Handle #include after & line continuation (#93382)Peter Klausler1-9/+10
Some applications like to use a CPP-style #include directive to pull in a common list of arguments, dummy arguments, or COMMON block variables after a free-form & line continuation marker. This works naturally with compilers that run an actual cpp pass over the input before doing anything specific to Fortran, but it's a case that I missed with this integrated preprocessor.
2024-05-01[flang] Ensure all warning/portability messages are guarded by Should… ↵Peter Klausler1-12/+27
(#90518) …Warn() Many warning messages were being emitted unconditionally. Ensure that all warnings are conditional on a true result from a call to common::LanguageFeatureControl::ShouldWarn() so that it is easy for a driver to disable them all, or, in the future, to provide per-warning control over them.
2024-04-10[flang][Frontend] Implement printing defined macros via -dM (#87627)Krzysztof Parzyszek1-7/+61
This should work the same way as in clang.
2024-02-20[flang] Resolve "possible performance problem" issue spam (#79769)Peter Klausler1-1/+1
Four "issues" on GitHub report possible performance problems, likely detected by static analysis. None of them would ever make a measureable difference in compilation time, but I'm resolving them to clean up the open issues list. Fixes https://github.com/llvm/llvm-project/issues/79703, .../79705, .../79706, & .../79707.
2024-02-03[flang] Simplify a string comparison (NFC)Kazu Hirata1-1/+1
2024-01-31[flang][preprocessor] Replace macros in some #include directives (#80039)Peter Klausler1-17/+24
Ensure that #include FOO undergoes macro replacement. But, as is the case with C/C++, continue to not perform macro replacement in a #include directive with <angled brackets>.
2023-11-30[flang] Handle preprocessor macro expansion edge case (#73835)Peter Klausler1-99/+133
When a reference to a function-like macro begins during the rescanning of the expansion of another macro but is not completed by the end of that expansion, it is necessary to abort that rescanning of that expansion and try again when more tokens can be acquired. (See the new unclosed-FLM.F90 test case.) All other Fortran preprocessors to which I have access can handle this situation.
2023-11-13[flang][preprocessor] Finesse disabling of function-like macros (#71589)Peter Klausler1-2/+2
During function-like macro expansion in a standard C/C++ preprocessor, the macro being expanded is disabled from recursive macro expansion. The implementation in this compiler's preprocessor, however, was too broad; the macro expansion needs to be disabled for the "rescanning" phase only, not for actual argument expansion. (Also corrects an obsolete comment elsewhere that was noticed during reduction of an original test case.)
2023-08-23[flang] Fix crash from empty -DMACRO= (bug #64837)Peter Klausler1-9/+11
Some vector indexing code in the preprocessor fails with empty tokens or token sequences in predefined macros. Fixes https://github.com/llvm/llvm-project/issues/64837. Differential Revision: https://reviews.llvm.org/D158451
2023-08-23[flang][Preprocessor] Constrain a bit more implicit continuationsRoger Ferrer Ibanez1-21/+5
D155499 fixed an issue with implicit continuations. The fixes included a nested parenthesis check during definition of a macro which is then carried over in the scanner state. This leads to the following corner case to fail: subroutine foo(a, d) implicit none integer :: a integer :: d ! An implicit continuation won't be considered unless ! the definition of "bar" above is removed/commented call sub(1, 2) end subroutine foo The definition of bar is indeed unbalanced but it is not even used in the code, so it should not impact whether we apply implicit continuation in the expansion of sub. This change aims at addressing this issue by removing the balance check and constraining a bit more when we consider implicit continuations: only when we see a left parenthesis after a function-like macro, not a object-like macro. In this case I think it is OK to (unconditionally) implicitly continue to the next line in search of the corresponding right parenthesis. This is, to my understanding, similar to what the C preprocessor would do according to the description in [1]. [1] https://www.spinellis.gr/blog/20060626/ Differential Revision: https://reviews.llvm.org/D157414
2023-07-31[flang] Stricter "implicit continuation" in preprocessingPeter Klausler1-32/+56
The prescanner performs implicit line continuation when it looks like the parenthesized arguments of a call to a function-like macro may span multiple lines. In an attempt to work more like a Fortran-oblivious C preprocessor, the prescanner will act as if the following lines had been continuations so that the function-like macro could be invoked. This still seems like a good idea, but a recent bug report on LLVM's GitHub issue tracker shows one way in which it could trigger inadvertently and mess up a program. So this patch makes the conditions for implicit line continuation much more strict. First, the leading parenthesis has to have been preceded by an identifier that's known to be a macro name. (It doesn't have to be a function-like macro, since it's possible for a keyword-like macro to expand to the name of a function-like macro.) Second, no macro definition can ever have had unbalanced parentheses in its replacement text. Also cleans up some parenthesis recognition code to fix some issues found in testing, so that a token with leading or trailing spaces can still be recognized as a parenthesis or comma. Fixes https://github.com/llvm/llvm-project/issues/63844. Differential Revision: https://reviews.llvm.org/D155499
2023-06-29[flang] Honor #line and related preprocessing directivesPeter Klausler1-2/+56
Extend the SourceFile class to take account of #line directives when computing source file positions for error messages. Adjust the output of #line directives to -E output so that they reflect any #line directives that were in the input. Differential Revision: https://reviews.llvm.org/D153910
2023-05-31[flang] CUDA Fortran - part 1/5: parsingPeter Klausler1-4/+5
Begin upstreaming of CUDA Fortran support in LLVM Flang. This first patch implements parsing for CUDA Fortran syntax, including: - a new LanguageFeature enum value for CUDA Fortran - driver change to enable that feature for *.cuf and *.CUF source files - parse tree representation of CUDA Fortran syntax - dumping and unparsing of the parse tree - the actual parsers for CUDA Fortran syntax - prescanning support for !@CUF and !$CUF - basic sanity testing via unparsing and parse tree dumps ... along with any minimized changes elsewhere to make these work, mostly no-op cases in common::visitors instances in semantics and lowering to allow them to compile in the face of new types in variant<> instances in the parse tree. Because CUDA Fortran allows the kernel launch chevron syntax ("call foo<<<blocks, threads>>>()") only on CALL statements and not on function references, the parse tree nodes for CallStmt, FunctionReference, and their shared Call were rearranged a bit; this caused a fair amount of one-line changes in many files. More patches will follow that implement CUDA Fortran in the symbol table and name resolution, and then semantic checking. Differential Revision: https://reviews.llvm.org/D150159
2023-05-22[flang][preprocessing] Allow keyword macro to rename a function-like macroPeter Klausler1-23/+44
#define FOO(x) ((x)+1) #define BAR FOO print *, BAR(1) should work as one would expect. Fixes https://github.com/llvm/llvm-project/issues/47162. Differential Revision: https://reviews.llvm.org/D151154
2023-02-18[flang] Remove macro replacement in angular bracket includesEthan Luis McDonough1-1/+1
Addresses Github issue [[ https://github.com/llvm/llvm-project/issues/60317 | #60317 ]]. Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D143469
2022-03-08[flang] Distinguish usage and portability warning messagesPeter Klausler1-8/+11
Using recently established message severity codes, upgrade non-fatal messages to usage and portability warnings as appropriate. Differential Revision: https://reviews.llvm.org/D121246
2022-01-12[flang] Fix handling of space between # and name in preprocessor stringificationPeter Klausler1-1/+2
When preprocessing "# ARG" in function-like macro expansion, the preprocessor needs to pop the previously-pushed '#' token from the end of the resulting token sequence after detecting the argument name. The code to do this was just wrong in a couple of ways. Differential Revision: https://reviews.llvm.org/D117148
2021-09-02[flang] NFC: change non-nullable pointer arguments to referencespeter klausler1-44/+43
Ticking off a Parser TODO: Preprocessor::Directive()'s Prescanner argument should be a reference, not a pointer. Differential Revision: https://reviews.llvm.org/D109094
2021-02-11[flang] Don't perform macro replacement unless *.F, *.F90, &c.peter klausler1-9/+10
Avoid spurious and confusing macro replacements from things like -DPIC on Fortran source files whose suffixes indicate that preprocessing is not expected. Add gfortran-like "-cpp" and "-nocpp" flags to f18 to force predefinition of macros independent of the source file suffix. Differential Revision: https://reviews.llvm.org/D96464
2021-01-27[flang] Search for #include "file" in right directory (take 2)peter klausler1-1/+10
Make the #include "file" preprocessing directive begin its search in the same directory as the file containing the directive, as other preprocessors and our Fortran INCLUDE statement do. Avoid current working directory for all source files except the original. Resolve tests. Differential Revision: https://reviews.llvm.org/D95481
2021-01-26Revert "[flang] Search for #include "file" in right directory"Andrzej Warzynski1-9/+1
This reverts commit d987b61b1dce9948801ac37704477e7c257100b1. As pointed out in https://reviews.llvm.org/D95388, the reverted commit causes build failures in the following Flang buildbots: * http://lab.llvm.org:8011/#/builders/32/builds/2642 * http://lab.llvm.org:8011/#/builders/33/builds/2131 * http://lab.llvm.org:8011/#/builders/135/builds/1473 * http://lab.llvm.org:8011/#/builders/66/builds/1559 * http://lab.llvm.org:8011/#/builders/134/builds/1409 * http://lab.llvm.org:8011/#/builders/132/builds/1817 I'm guessing that the patch was only tested with `FLANG_BUILD_NEW_DRIVER=Off` (i.e. the default). The builders listed above set `FLANG_BUILD_NEW_DRIVER` to `On`. Although fixing the build is relatively easy, the reverted patch modifies the behaviour of the frontend, which breaks driver tests. In particular, in https://reviews.llvm.org/D93453 support for `-I` was added that depends on the current behaviour. The reverted patch changes that behaviour. Either the tests have to be updated or the change fine-tuned.
2021-01-25[flang] Search for #include "file" in right directorypeter klausler1-1/+9
Make the #include "file" preprocessing directive begin its search in the same directory as the file containing the directive, as other preprocessors and our Fortran INCLUDE statement do. Avoid current working directory for all source files after the original. Differential Revision: https://reviews.llvm.org/D95388
2020-09-18[flang] Rework preprocessing of stringificationpeter klausler1-43/+81
Hew more closely to the C17 standard; perform macro replacement of arguments to function-like macros unless they're being stringified or pasted. Test with a model "assert" macro idiom that exposed the problem. Differential Revision: https://reviews.llvm.org/D87650
2020-09-14[flang] Allow Fortran comments after #include pathpeter klausler1-7/+9
C-style /*comments*/ are removed during preprocessing directive tokenization, but Fortran !comments need to be specifically allowed. Fixes LLVM bugzilla 47466. Differential Revision: https://reviews.llvm.org/D87638
2020-08-13[flang] Ensure Preprocessor::Define saves macro names correctlypeter klausler1-1/+1
This fixes problems with macros defined with -D on the command line and predefined macros defined in the throwaway driver program.
2020-08-03[flang] Make preprocessing behavior tests runnable as regression testspeter klausler1-1/+1
And fix a minor bug exposed by doing so. Differential Revision: https://reviews.llvm.org/D85164
2020-07-17[flang] Allow ! and // comments after some preprocessing directivespeter klausler1-9/+7
Old-style C /*comments*/ are omitted from preprocessor directive token sequences by the prescanner, but line-ending C++ and Fortran free-form comments are not since their handling might depend on the directive. Add code to skip these line-ending comments as appropriate in place of existing code that just skipped blanks. Reviewed By: sscalpone Differential Revision: https://reviews.llvm.org/D84061
2020-03-28[flang] Reformat with latest clang-format and .clang-formatTim Keith1-41/+76
Original-commit: flang-compiler/f18@9fe84f45d7fd685051004678d6b5775dcc4c6f8f Reviewed-on: https://github.com/flang-compiler/f18/pull/1094
2020-03-19[flang] [LLVMify F18] Replace the use std::ostream with LLVM streams ↵Caroline Concatto1-4/+6
llvm::ostream This patch replaces the occurrence of std::ostream by llvm::raw_ostream. In LLVM Coding Standards[1] "All new code should use raw_ostream instead of ostream".[1] As a consequence, this patch also replaces the use of: std::stringstream by llvm::raw_string_ostream or llvm::raw_ostream* std::ofstream by llvm::raw_fd_ostream std::endl by '\n' and flush()[2] std::cout by llvm::outs() and std::cerr by llvm::errs() It also replaces std::strerro by llvm::sys::StrError** , but NOT in Fortran runtime libraries *std::stringstream were replaced by llvm::raw_ostream in all methods that used std::stringstream as a parameter. Moreover, it removes the pointers to these streams. [1]https://llvm.org/docs/CodingStandards.html [2]https://releases.llvm.org/2.5/docs/CodingStandards.html#ll_avoidendl Signed-off-by: Caroline Concatto <caroline.concatto@arm.com> Running clang-format-7 Signed-off-by: Caroline Concatto <caroline.concatto@arm.com> Removing residue of ostream library Signed-off-by: Caroline Concatto <caroline.concatto@arm.com> Original-commit: flang-compiler/f18@a3507d44b8911e6024033aa583c1dc54e0eb89fd Reviewed-on: https://github.com/flang-compiler/f18/pull/1047
2020-02-25[flang] [LLVMify F18] Compiler module folders should have capitalised names ↵CarolineConcatto1-0/+1015
(flang-compiler/f18#980) This patch renames the modules in f18 to use a capital letter in the module name Signed-off-by: Caroline Concatto <caroline.concatto@arm.com> Original-commit: flang-compiler/f18@d2eb7a1c443d1539ef12b6f027074a0eb15b1ea0 Reviewed-on: https://github.com/flang-compiler/f18/pull/980