aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/PrintPreprocessedOutput.cpp
AgeCommit message (Collapse)AuthorFilesLines
2022-12-20[Clang] Prepare for llvm::Optional becoming std::optional.Benjamin Kramer1-10/+4
The needed tweaks are mostly trivial, the one nasty bit is Clang's usage of OptionalStorage. To keep this working old Optional stays around as clang::CustomizableOptional, with the default Storage removed. Optional<File/DirectoryEntryRef> is replaced with a typedef. I tested this with GCC 7.5, the oldest supported GCC I had around. Differential Revision: https://reviews.llvm.org/D140332
2022-12-18Revert "[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to ↵Krzysztof Parzyszek1-9/+12
std::optional" This reverts commit 8f0df9f3bbc6d7f3d5cbfd955c5ee4404c53a75d. The Optional*RefDegradesTo*EntryPtr types want to keep the same size as the underlying type, which std::optional doesn't guarantee. For use with llvm::Optional, they define their own storage class, and there is no way to do that in std::optional. On top of that, that commit broke builds with older GCCs, where std::optional was not trivially copyable (static_assert in the clang sources was failing).
2022-12-17[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to std::optionalKrzysztof Parzyszek1-12/+9
2022-09-08[clang] Use std::size instead of llvm::array_lengthofJoe Loser1-1/+1
LLVM contains a helpful function for getting the size of a C-style array: `llvm::array_lengthof`. This is useful prior to C++17, but not as helpful for C++17 or later: `std::size` already has support for C-style arrays. Change call sites to use `std::size` instead. Leave the few call sites that use a locally defined `array_lengthof` that are meant to test previous bugs with NTTPs in clang analyzer and SemaTemplate. Differential Revision: https://reviews.llvm.org/D133520
2022-04-14[clang][lex] NFCI: Use FileEntryRef in PPCallbacks::InclusionDirective()Jan Svoboda1-4/+4
This patch changes type of the `File` parameter in `PPCallbacks::InclusionDirective()` from `const FileEntry *` to `Optional<FileEntryRef>`. With the API change in place, this patch then removes some uses of the deprecated `FileEntry::getName()` (e.g. in `DependencyGraph.cpp` and `ModuleDependencyCollector.cpp`). Reviewed By: dexonsmith, bnbarham Differential Revision: https://reviews.llvm.org/D123574
2022-03-28[C++20][Modules][HU 5/5] Add fdirectives-only mode for preprocessing output.Iain Sandoe1-9/+24
When the -fdirectives-only option is used together with -E, the preprocessor output reflects evaluation of if/then/else directives. As such, it preserves defines and undefs of macros that are still live after such processing. The intent is that this output could be consumed as input to generate considered a C++20 header unit. We strip out any (unused) defines that come from built-in, built-in-file or command line; these are re-added when the preprocessed source is consumed. Differential Revision: https://reviews.llvm.org/D121099
2022-02-02[clang] fix out of bounds access in an empty string when lexing a _Pragma ↵Alex Lorenz1-1/+2
with missing string token The lexer can attempt to lex a _Pragma and crash with an out of bounds string access when it's lexing a _Pragma whose string token is an invalid buffer, e.g. when a module header file from which the macro expansion for that token was deleted from the file system. Differential Revision: https://reviews.llvm.org/D116052
2022-01-26Revert "Rename llvm::array_lengthof into llvm::size to match std::size from ↵Benjamin Kramer1-1/+1
C++17" This reverts commit ef8206320769ad31422a803a0d6de6077fd231d2. - It conflicts with the existing llvm::size in STLExtras, which will now never be called. - Calling it without llvm:: breaks C++17 compat
2022-01-26Rename llvm::array_lengthof into llvm::size to match std::size from C++17serge-sans-paille1-1/+1
As a conquence move llvm::array_lengthof from STLExtras.h to STLForwardCompat.h (which is included by STLExtras.h so no build breakage expected).
2022-01-09[clang] Use true/false instead of 1/0 (NFC)Kazu Hirata1-1/+1
Identified with modernize-use-bool-literals.
2021-11-05[Preprocessor] Fix newline before/after _Pragma.Michael Kruse1-6/+4
The PragmaAssumeNonNullHandler (and maybe others) passes an invalid SourceLocation to its callback, hence PrintPreprocessedOutput does not know how many lines to insert between the previous token and the pragma and does nothing. With this patch we instead assume that the unknown token is on the same line as the previous such that we can call the procedure that also emits semantically significant whitespace. Fixes bug reported here: https://reviews.llvm.org/D104601#3105044
2021-11-05[Preprocessor] Fix warning: left and right subexpressions are identical. NFCI.Michael Kruse1-1/+1
This is reported by msvc as warning C6287: redundant code: the left and right subexpressions are identical EmittedDirectiveOnThisLine implies EmittedTokensOnThisLine making this an NFC change. To be on the safe side and because both of them are checked at other places as well, we continue to check both. Compiler warning reported here: https://reviews.llvm.org/D104601#2957333
2021-09-28[clang] Let PPCallbacks::PragmaWarning() pass specifier as enum instead of ↵Nico Weber1-3/+17
string Differential Revision: https://reviews.llvm.org/D110635
2021-08-25[Preprocessor] Elide empty line(s) at start of file.Michael Kruse1-1/+1
In -P mode, PrintPPOutputPPCallbacks::MoveToLine started at least one newline if current and target line number mismatched. The method is also called when entering a new file, be it the main file or an include file. In this situation line numbers always almost mismatch, resulting in a newline for each occurance even if no tokens have been printed in-between. Empty lines at the beginning of the output must be trimmed because it may be parsed by scripts expecting the result to appear on the first output line, as done by LibreOffice's configure script. Fix by only emitting a newline if tokens have been printed so far using the EmittedTokensOnThisLine flag. Also adding a test case of FileChanged callbacks occuring with empty include files. This fixes llvm.org/PR51616
2021-08-19Fix unknown parameter Wdocumentation warning. NFC.Simon Pilgrim1-1/+1
2021-08-01[Preprocessor] Ensure newline after #pragma introduced by -fms-extensions.Michael Kruse1-1/+3
The -fms-extensions converts __pragma (and _Pragma) into a #pragma that has to occur at the beginning of a line and end with a newline. This patch ensures that the newline after the #pragma is added even if Token::isAtStartOfLine() indicated that we should not start a newline. Committing relying post-commit review since the change is small, some downstream uses might be blocked without this fix, and to make clear the decision of the new -fminimize-whitespace feature (fix on main, revert on clang-13.x branch) suggested by @aaron.ballman in D104601. Differential Revision: https://reviews.llvm.org/D107183
2021-07-28[Preprocessor] -E -P: Ensure newline after 8 skipped lines.Michael Kruse1-8/+15
The implementation of -fminimize-whitespace (D104601) revised the logic when to emit newlines. There was no case to handle when more than 8 lines were skippped in -P (DisableLineMarkers) mode and instead fell through the case intended for -fminimize-whitespace, i.e. emit nothing. This patch will emit one newline in this case. The newline logic is slightly reorganized. The `-P -fminimize-whitespace` case is handled explicitly and emitting at least one newline is the new fallback case. The choice between emitting a line marker or up to 7 empty lines is now a choice only with enabled line markers. The up to 8 newlines likely are fewer characters than a line directive, but in -P mode this had the paradoxic effect that it would print up to 7 empty lines, but none at all if more than 8 lines had to be skipped. Now with DisableLineMarkers, we don't consider printing empty lines (just start a new line) which matches gcc's behavior. The line-directive-output-mincol.c test is replaced with a more comprehensive test skip-empty-lines.c also testing the more than 8 skipped lines behaviour with all flag combinations. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D106924
2021-07-25[Preprocessor] Implement -fminimize-whitespace.Michael Kruse1-141/+208
This patch adds the -fminimize-whitespace with the following effects: * If combined with -E, remove as much non-line-breaking whitespace as possible. * If combined with -E -P, removes as much whitespace as possible, including line-breaks. The motivation is to reduce the amount of insignificant changes in the preprocessed output with source files where only whitespace has been changed (add/remove comments, clang-format, etc.) which is in particular useful with ccache. A patch for ccache for using this flag has been proposed to ccache as well: https://github.com/ccache/ccache/pull/815, which will use -fnormalize-whitespace when clang-13 has been detected, and additionally uses -P in "unify_mode". ccache already had a unify_mode in an older version which was removed because of problems that using the preprocessor itself does not have (such that the custom tokenizer did not recognize C++11 raw strings). This patch slightly reorganizes which part is responsible for adding newlines that are required for semantics. It is now either startNewLineIfNeeded() or MoveToLine() but never both; this avoids the ShouldUpdateCurrentLine workaround and avoids redundant lines being inserted in some cases. It also fixes a mandatory newline not inserted after a _Pragma("...") that is expanded into a #pragma. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D104601
2019-08-14[Clang] Migrate llvm::make_unique to std::make_uniqueJonas Devlieghere1-1/+1
Now that we've moved to C++14, we no longer need the llvm::make_unique implementation from STLExtras.h. This patch is a mechanical replacement of (hopefully) all the llvm::make_unique instances across the monorepo. Differential revision: https://reviews.llvm.org/D66259 llvm-svn: 368942
2019-05-21[PragmaHandler] Expose `#pragma` locationJoel E. Denny1-1/+1
Currently, a pragma AST node's recorded location starts at the namespace token (such as `omp` in the case of OpenMP) after the `#pragma` token, and the `#pragma` location isn't available. However, the `#pragma` location can be useful when, for example, rewriting a directive using Clang's Rewrite facility. This patch makes `#pragma` locations available in any `PragmaHandler` but it doesn't yet make use of them. This patch also uses the new `struct PragmaIntroducer` to simplify `Preprocessor::HandlePragmaDirective`. It doesn't do the same for `PPCallbacks::PragmaDirective` because that changes the API documented in `clang-tools-extra/docs/pp-trace.rst`, and I'm not sure about backward compatibility guarantees there. Reviewed By: ABataev, lebedev.ri, aaron.ballman Differential Revision: https://reviews.llvm.org/D61643 llvm-svn: 361335
2019-05-17[Lex] Allow to consume tokens while preprocessingIlya Biryukov1-1/+2
Summary: By adding a hook to consume all tokens produced by the preprocessor. The intention of this change is to make it possible to consume the expanded tokens without re-runnig the preprocessor with minimal changes to the preprocessor and minimal performance penalty when preprocessing without recording the tokens. The added hook is very low-level and reconstructing the expanded token stream requires more work in the client code, the actual algorithm to collect the tokens using this hook can be found in the follow-up change. Reviewers: rsmith Reviewed By: rsmith Subscribers: eraman, nemanjai, kbarton, jsji, riccibruno, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59885 llvm-svn: 361007
2019-04-11[C++20] Implement context-sensitive header-name lexing and pp-import parsing ↵Richard Smith1-2/+11
in the preprocessor. llvm-svn: 358231
2019-03-14Add PragmaHandler for MSVC pragma execution_character_setReid Kleckner1-0/+20
__pragma(execution_character_set(push, "UTF-8")) is used in TraceLoggingProvider.h. This commit implements a no-op handler for compatability, similar to how the flag -fexec_charset is handled. Patch by Matt Gardner! Differential Revision: https://reviews.llvm.org/D58530 llvm-svn: 356185
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-11-15Fix combining pragma __debug dump & parser_crash with -EDavid Blaikie1-0/+5
Previously these would be transformed into annotation tokens and the preprocessor would then assume they were real tokens with source locations and assert/UB. Other pragmas that produce annotation tokens aren't a problem because they aren't handled if the parser isn't hooked up - ParsePragma.cpp registers those handlers & isn't run for pure preprocessing. So they're treated as unknown pragmas & printed verbatim by the preprocessor. Perhaps these pragmas should be treated the same way? But they got mixed in with other __debug pragmas that do need to be handled during preprocessing. The third __debug pragma that produces an annotation token is 'captured' - which had its own fix for this issue - by not inserting the annotation token in the first place if it detected that it was in preprocessing mode. I've removed that fix (from Lex/Pragma.cpp) in favor of the more general one in Frontend/PrintPreprocessedOutput.cpp. llvm-svn: 346928
2018-07-30Remove trailing spaceFangrui Song1-6/+6
sed -Ei 's/[[:space:]]+$//' include/**/*.{def,h,td} lib/**/*.{cpp,h} llvm-svn: 338291
2018-05-10Reland '[clang] Adding CharacteristicKind to PPCallbacks::InclusionDirective'Julie Hockett1-10/+13
This commit relands r331904. Adding a SrcMgr::CharacteristicKind parameter to the InclusionDirective in PPCallbacks, and updating calls to that function. This will be useful in https://reviews.llvm.org/D43778 to determine which includes are system headers. Differential Revision: https://reviews.llvm.org/D46614 llvm-svn: 332021
2018-05-09Revert "[clang] Adding CharacteristicKind to PPCallbacks::InclusionDirective"Julie Hockett1-13/+10
This reverts commit r331904 because of a memory leak. llvm-svn: 331932
2018-05-09[clang] Adding CharacteristicKind to PPCallbacks::InclusionDirectiveJulie Hockett1-10/+13
Adding a SrcMgr::CharacteristicKind parameter to the InclusionDirective in PPCallbacks, and updating calls to that function. This will be useful in https://reviews.llvm.org/D43778 to determine which includes are system headers. Differential Revision: https://reviews.llvm.org/D46614 llvm-svn: 331904
2018-01-03Calculate size of buffer instead of using a magic value.Paul Robinson1-1/+1
Patch by Matthew Davis! Differential Revision: https://reviews.llvm.org/D41421 llvm-svn: 321757
2017-10-16Don't print end-of-directive tokens in -E outputReid Kleckner1-0/+6
This comes up when pre-processing standalone .s files containing hash-prefixed comments. The pre-processor should skip the unknown directive and not emit an extra newline as we were doing. Fixes PR34950 llvm-svn: 315953
2017-09-27[Preprocessor] Preserve #pragma clang assume_nonnull in preprocessed outputEli Friedman1-0/+18
Patch by Zbigniew Sarbinowski! Differential Revision: https://reviews.llvm.org/D37861 llvm-svn: 314364
2017-07-17[NFC] Refactor the Preprocessor function that handles Macro definitions and ↵Faisal Vali1-2/+2
rename Arguments to Parameters in Macro Definitions. - Extracted the reading of the tokens out into a separate function. - Replace 'Argument' with 'Parameter' when referring to the identifiers of the macro definition (as opposed to the supplied arguments - MacroArgs - during the macro invocation). This is in preparation for submitting patches for review to implement __VA_OPT__ which will otherwise just keep lengthening the HandleDefineDirective function and making it less comprehensible. I will also directly update some extra clang tooling that is broken by the change from Argument to Parameter. Hopefully the bots will stay appeased. Thanks! llvm-svn: 308190
2017-07-17Revert changes from my previous refactoring - will need to fix dependencies ↵Faisal Vali1-2/+2
in clang's extra tooling (such as clang-tidy etc.). Sorry about that. llvm-svn: 308158
2017-07-17[NFC] Refactor the Preprocessor function that handles Macro definitions and ↵Faisal Vali1-2/+2
rename Arguments to Parameters in Macro Definitions. - Extracted the reading of the tokens out into a separate function. - Replace 'Argument' with 'Parameter' when referring to the identifiers of the macro definition (as opposed to the supplied arguments - MacroArgs - during the macro invocation). This is in preparation for submitting patches for review to implement __VA_OPT__ which will otherwise just keep lengthening the HandleDefineDirective function and making it less comprehensible. Thanks! llvm-svn: 308157
2017-06-19Support non-identifier module names when preprocessing modules.Richard Smith1-3/+3
llvm-svn: 305758
2017-05-04Add #pragma clang module begin/end pragmas and generate them when ↵Richard Smith1-3/+34
preprocessing a module. These pragmas are intended to simulate the effect of entering or leaving a file with an associated module. This is not completely implemented yet: declarations between the pragmas will not be attributed to the correct module, but macro visibility is already functional. Modules named by #pragma clang module begin must already be known to clang (in some module map that's either loaded or on the search path). llvm-svn: 302098
2017-04-29Add pragma to perform module import and use it in -E output.Richard Smith1-30/+37
Many of our supported configurations support modules but do not have any first-class syntax to perform a module import. This leaves us with a problem: there is no way to represent the expansion of a #include that imports a module in the -E output for such languages. (We don't want to just leave it as a #include because that requires the consumer of the preprocessed source to have the same file system layout and include paths as the creator.) This patch adds a new pragma: #pragma clang module import MODULE.NAME.HERE that imports a module, and changes -E and -frewrite-includes to use it when rewriting a #include that maps to a module import. We don't make any attempt to use a native language syntax import if one exists, to get more consistent output. (If in the future, @import and #include have different semantics in some way, the pragma will track the #include semantics.) llvm-svn: 301725
2017-04-27Remove leaking UnknownPragmaHandlers right after we are done with them.Vassil Vassilev1-13/+27
The UnknownPragmaHandlers added by DoPrintPreprocessedInput conflict with the real PragmaHandlers from clang::Parser because they try to handle the same #pragma directives. This makes it impossible to use a Preprocessor (that was previously passed to DoPrintPreprocessedInput), as an Preprocessor for a clang::Parser instance which is what we currently do in cling. This patch removes the added UnknownPragmaHandler to avoid conflicts these conflicts and leave the PragmaHandlers of the Preprocessors in a the same state as before calling DoPrintPreprocessedInput. Patch by Raphael Isemann (D32486)! llvm-svn: 301563
2017-04-26Revert "Revert "PPCallbacks::MacroUndefined, change signature and add test.""Vedant Kumar1-2/+4
This reverts commit r301469. It isn't needed with r301470, which fixes the API break introduced in the original commit. llvm-svn: 301472
2017-04-26Revert "PPCallbacks::MacroUndefined, change signature and add test."Vedant Kumar1-4/+2
This reverts commit r301449. It breaks the build with: MacroPPCallbacks.h:114:50: error: non-virtual member function marked 'override' hides virtual member function llvm-svn: 301469
2017-04-26PPCallbacks::MacroUndefined, change signature and add test.Frederich Munch1-2/+4
Summary: The PPCallbacks::MacroUndefined callback is currently insufficient for clients that need to track the MacroDirectives. This patch adds an additional argument to PPCallbacks::MacroUndefined that is the undef MacroDirective. Reviewers: bruno, manmanren Reviewed By: bruno Subscribers: nemanjai, cfe-commits Differential Revision: https://reviews.llvm.org/D29923 llvm-svn: 301449
2016-11-17[Preprocessor] Support for '-dI' flagBruno Cardoso Lopes1-8/+26
Re-introduce r285411. Implement the -dI as supported by GCC: Output ‘#include’ directives in addition to the result of preprocessing. This change aims to add this option, pass it through to the preprocessor via the options class, and when inclusions occur we output some information (+ test cases). Patch by Steve O'Brien! Differential Revision: https://reviews.llvm.org/D26089 llvm-svn: 287275
2016-10-28Revert "[Preprocessor] Support for '-dI' flag"Bruno Cardoso Lopes1-26/+8
This reverts r285411. Tests failing on http://lab.llvm.org:8011/builders/clang-x86-windows-msvc2015/builds/141 llvm-svn: 285416
2016-10-28[Preprocessor] Support for '-dI' flagBruno Cardoso Lopes1-8/+26
Implement the -dI as supported by GCC: Output ‘#include’ directives in addition to the result of preprocessing. This change aims to add this option, pass it through to the preprocessor via the options class, and when inclusions occur we output some information (+ test cases). Patch by Steve O'Brien! Differential Revision: https://reviews.llvm.org/D25153 llvm-svn: 285411
2016-04-08[modules] Add a comment to explain why -E leaves some #includes in the ↵Richard Smith1-1/+3
preprocessed output. llvm-svn: 265766
2016-04-08[modules] Don't write @import in -E output if the current language mode doesn'tRichard Smith1-2/+12
support @import; use the form as written instead. llvm-svn: 265756
2016-02-18Remove use of builtin comma operator.Richard Trieu1-2/+4
Cleanup for upcoming Clang warning -Wcomma. No functionality change intended. llvm-svn: 261271
2016-02-13Reduce the number of implicit StringRef->std::string conversions by ↵Benjamin Kramer1-12/+10
threading StringRef through more APIs. No functionality change intended. llvm-svn: 260815
2016-02-09Simplify EnterTokenStream API to make it more robust for memory managementDavid Blaikie1-4/+3
While this won't help fix things like the bug that r260219 addressed, it seems like good tidy up to have anyway. (it might be nice if "makeArrayRef" always produced a MutableArrayRef & let it decay to an ArrayRef when needed - then I'd use that for the MutableArrayRefs in this patch) If we had std::dynarray I'd use that instead of unique_ptr+size_t, ideally (but then it'd have to be threaded down through the Preprocessor all the way - no idea how painful that would be) llvm-svn: 260246