Commit Graph

3086 Commits

Author SHA1 Message Date
Benjamin Kramer 8956852194 Undo a bit of fcf4e360ba that confuses MSVC
clang\lib\Serialization\GlobalModuleIndex.cpp(818): error C2440: 'initializing': cannot convert from 'const ValueTy' to '_Ty2 &&'
        with
        [
            ValueTy=llvm::SmallVector<unsigned int,2>
        ]
        and
        [
            _Ty2=llvm::SmallVector<unsigned int,2>
        ]
2022-12-04 20:33:25 +01:00
Benjamin Kramer fcf4e360ba Iterate over StringMaps using structured bindings. NFCI. 2022-12-04 18:36:41 +01:00
Kazu Hirata 5891420e68 [clang] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:46 -08:00
Jan Svoboda bfab778e95 [clang][modules] Serialize the new RecordID
This is a follow-up to commit 40472ef14c that introduced new record but didn't serialize its ID.
2022-12-02 23:27:48 -08:00
Vassil Vassilev dc4889357a [clang-repl] Support statements on global scope in incremental mode.
This patch teaches clang to parse statements on the global scope to allow:
```
./bin/clang-repl
clang-repl> int i = 12;
clang-repl> ++i;
clang-repl> extern "C" int printf(const char*,...);
clang-repl> printf("%d\n", i);
13
clang-repl> %quit
```

Generally, disambiguating between statements and declarations is a non-trivial
task for a C++ parser. The challenge is to allow both standard C++ to be
translated as if this patch does not exist and in the cases where the user typed
a statement to be executed as if it were in a function body.

Clang's Parser does pretty well in disambiguating between declarations and
expressions. We have added DisambiguatingWithExpression flag which allows us to
preserve the existing and optimized behavior where needed and implement the
extra rules for disambiguating. Only few cases require additional attention:
  * Constructors/destructors -- Parser::isConstructorDeclarator was used in to
    disambiguate between ctor-looking declarations and statements on the global
    scope(eg. `Ns::f()`).
  * The template keyword -- the template keyword can appear in both declarations
    and statements. This patch considers the template keyword to be a declaration
    starter which breaks a few cases in incremental mode which will be tackled
    later.
  * The inline (and similar) keyword -- looking at the first token in many cases
    allows us to classify what is a declaration.
  * Other language keywords and specifiers -- ObjC/ObjC++/OpenCL/OpenMP rely on
    pragmas or special tokens which will be handled in subsequent patches.

The patch conceptually models a "top-level" statement into a TopLevelStmtDecl.
The TopLevelStmtDecl is lowered into a void function with no arguments.
We attach this function to the global initializer list to execute the statement
blocks in the correct order.

Differential revision: https://reviews.llvm.org/D127284
2022-12-03 07:18:07 +00:00
Jan Svoboda 7806a92868 [clang][modules][deps] Serialize inputs into PCMs using the "as requested" name
This patch changes the PCM serialization logic to refer to input files by their "requested" name. This fixes a bug where the dependency scanner reports the "final" file paths, which can result in failed explicit compiles due to the `module.modulemap` file not being surrounded by the expected framework directory structure.

Depends on D135634.

Reviewed By: benlangmuir, Bigcheese

Differential Revision: https://reviews.llvm.org/D135636
2022-12-02 16:12:16 -08:00
Jan Svoboda 40472ef14c [clang][modules] Serialize VFS overlay paths into PCMs
With implicitly built modules, the importing `CompilerInstance` assumes PCMs were built in a "compatible way" (i.e. with similarly set up instance). Either because their context hash matches, or because this instance has just built them.

There are some use-cases, however, where this assumption doesn't hold, libclang/c-index-test being one of them. There, the importing instance (or `ASTUnit`) is being set up while the PCM file is being deserialized. Until now, we've assumed the serialized paths to input files are the actual on-disk files, meaning the default physical VFS was always able to resolve them. This won't be the case after D135636. Therefore, this patch makes sure `ASTUnit` is initialized with the same VFS as the PCM it's deserializing - by storing paths to the VFS overlay files into the PCM itself.

For the VFS overlay files to be adopted at the very start of PCM deserialization, they are stored in a new section in the unhashed control block, together with header search paths and system header prefixes. The move to the unhashed control block should be safe: if two modules were built with different header search paths and they produced different results, the hashed part of the PCM file will reflect that.

Reviewed By: akyrtzi, benlangmuir

Differential Revision: https://reviews.llvm.org/D135634
2022-12-02 16:12:16 -08:00
Jan Svoboda abf0c6c0c0 Use CTAD on llvm::SaveAndRestore
Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D139229
2022-12-02 15:36:12 -08:00
Jan Svoboda 50fd6604b7 Reapply "[clang][modules][deps] Including module maps are affecting"
This reverts commit f0ce827c69.
This reapplies commit 83973cf157.
The new test now should pass on Windows thanks to commit 4d6483e91b.
2022-12-02 10:46:48 -08:00
Jan Svoboda 4d6483e91b Reapply "[clang][modules][deps] Parent module maps are affecting"
This reverts commit 67f34054d6.
This reapplies commit f99e5a9106.
This improves commit 8ab388e158 that unsucessfully attempted to forward-fix Windows test failure.
2022-12-02 10:32:47 -08:00
Jan Svoboda 67f34054d6 Revert "[clang][modules][deps] Parent module maps are affecting"
This reverts commit f99e5a9106.
2022-12-01 21:39:51 -08:00
Jan Svoboda f0ce827c69 Revert "[clang][modules][deps] Including module maps are affecting"
This reverts commit 83973cf157.
2022-12-01 21:39:51 -08:00
Jan Svoboda 83973cf157 [clang][modules][deps] Including module maps are affecting
With this patch, we mark module maps that include an affecting `extern` module map as also affecting. This is a generalization of D137197: now we don't require the importing module map to describe parent of the extern module.

Depends on D137198.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D137206
2022-12-01 20:16:17 -08:00
Jan Svoboda f99e5a9106 [clang][modules][deps] Parent module maps are affecting
Currently, the algorithm for gathering affecting module maps includes only those defining modules that include some headers. This is not entirely correct, though. Some module maps might be "importing" module maps for `extern` submodules. Such parent module maps are affecting - they do change semantics of the compilation. This patch adds parent module maps into the set of affecting module maps.

Depends on D137197.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D137198
2022-12-01 20:16:17 -08:00
Jan Svoboda ed07fe71d7 [clang][modules][deps] Transitive module maps are not affecting
Currently, the algorithm for gathering affecting module maps includes those defining transitive dependencies. This seems like an over-approximation, since those don't change the semantics of current module build.

(With this patch, `ModulesToProcess` only ever holds modules whose headers will be serialized into the current PCM.)

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D137197
2022-12-01 20:16:17 -08:00
Jan Svoboda 66cf61abd8 [clang][serialization] NFCI: Avoid re-reading input file info
This patch resolves a FIXME that points out an inefficiency in first deserializing the input file info and the whole input file, which redundantly deserializes the input file info again.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D137192
2022-12-01 20:07:01 -08:00
Corentin Jabot 9221bedfd7 Revert "Implement CWG2631"
This reverts commit 26fa17ed29.
This reverts commit 4403c4f9e7.

There is still an ODR issue causing linker errors, investigating.
2022-11-30 16:03:05 +01:00
Corentin Jabot 26fa17ed29 Implement CWG2631
Implement https://cplusplus.github.io/CWG/issues/2631.html.

Immediate calls in default arguments and defaults members
are not evaluated.

Instead, we evaluate them when constructing a
`CXXDefaultArgExpr`/`BuildCXXDefaultInitExpr`.

The immediate calls are executed by doing a
transform on the initializing expression.

Note that lambdas are not considering subexpressions so
we do not need to transform them.

As a result of this patch, unused default member
initializers are not considered odr-used, and
errors about members binding to local variables
in an outer scope only surface at the point
where a constructor is defined.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D136554
2022-11-30 11:25:01 +01:00
Mike Rice 530eb263c0 [clang] Add serialization for loop hint annotation tokens
When late parsed templates are used with PCH tokens are serialized. The
existing code does not handle annotation tokens which can occur due to
various pragmas.

This patch implements the serialization for annot_pragma_loop_hint.

This also enables use of OpenMP pragmas and #pragma unused which do not
need special serialization of the PtrData field.

Fixes https://github.com/llvm/llvm-project/issues/39504

Differential Revision: https://reviews.llvm.org/D138453
2022-11-29 10:51:11 -08:00
Alexander Kornienko 38f5ab4d44 Revert "[clang][modules] NFCI: Pragma diagnostic mappings: write/read FileID instead of SourceLocation"
This reverts commit f61c135a69. The commit affects
some build setups. See https://reviews.llvm.org/D137213#3939959.
2022-11-25 17:04:31 +01:00
Nathan James 15e76eed0c
[clang] Add [is|set]Nested methods to NamespaceDecl
Adds support for NamespaceDecl to inform if its part of a nested namespace.
This flag only corresponds to the inner namespaces in a nested namespace declaration.
In this example:
namespace <X>::<Y>::<Z> {}
Only <Y> and <Z> will be classified as nested.

This flag isn't meant for assisting in building the AST, more for static analysis and refactorings.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D90568
2022-11-24 12:44:35 +00:00
Fangrui Song 8f43b84322 [ASTReader] Fix zlib header detection 0x78 after D137885
D137885 picked an incorrect magic so it would fail when zstd is not
enabled.
2022-11-23 13:22:44 -08:00
Fangrui Song fa7bc386ec [modules] Support zstd in .pcm file
Extend SM_SLOC_BUFFER_BLOB_COMPRESSED to allow zstd, which is much faster
(compression/decompression) than zlib with a similar compression ratio.

An alternative is to add a value beside SM_SLOC_BUFFER_BLOB_COMPRESSED, but
reusing SM_SLOC_BUFFER_BLOB_COMPRESSED slightly simplifies the implementation
and leads to better diagnostics when a slightly older Clang consumes zstd
compressed blob.

Compressing AST takes a small portion of WriteAST, so we can pick a higher
compression level.

Compiling a relatively large .pcm (absl endian) with -fmodules-embed-all-files,
zstd level 9 has comparable performance with zlib-chromium level 6 (default),
but provides smaller output (5809156 => 5796016). Higher zstd levels will make
"Compress AST" notably slower and do not provide significant more size saving.

```
2.219345 Total ExecuteCompiler
0.746799 Total Frontend
0.736862 Total Source
0.339434 Total ReadAST
0.165452 Total WriteAST
0.043045 Total Compress AST
0.008236 Total ParseClass
0.00633 Total InstantiateClass
0.001887 Total isPotentialConstantExpr
0.001808 Total InstantiateFunction
0.001535 Total EvaluateForOverflow
0.000986 Total EvaluateAsRValue
0.000536 Total EvaluateAsBooleanCondition
0.000308 Total EvaluateAsConstantExpr
0.000156 Total EvaluateAsInt
3.4e-05 Total EvaluateKnownConstInt
8e-06 Total EvaluateAsInitializer
0 Total PerformPendingInstantiations
```

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D137885
2022-11-23 11:27:49 -08:00
Jennifer Yu 9d90cf2fca [OPENMP5.1] Initial support for message clause. 2022-11-18 17:59:23 -08:00
Fazlay Rabbi 56c1660170 [OpenMP] Initial parsing/sema for 'strict' modifier with 'num_tasks' clause
This patch gives basic parsing and semantic analysis support for 'strict'
modifier with 'num_tasks' clause of 'taskloop' construct introduced in
OpenMP 5.1 (section 2.12.2)

Differential Revision: https://reviews.llvm.org/D138328
2022-11-18 16:26:47 -08:00
Fazlay Rabbi ab9eac762c [OpenMP] Initial parsing/sema for 'strict' modifier with 'grainsize' clause
This patch gives basic parsing and semantic analysis support for 'strict'
modifier with 'grainsize' clause of 'taskloop' construct introduced in
OpenMP 5.1 (section 2.12.2)

Differential Revision: https://reviews.llvm.org/D138217
2022-11-17 20:59:07 -08:00
Chuanqi Xu d584468581 [C++20] [Modules] Don't emit macro definitions with named module
It is meaningless to emit macro definitions for named modules. With some
small experiments, the size of the module for the named modules reduced
2%~4% after this patch.
2022-11-18 11:11:17 +08:00
Jennifer Yu 1e054e6b52 [OPENMP5.1] Initial support for severity clause
Differential Revision:https://reviews.llvm.org/D138227
2022-11-17 16:05:02 -08:00
Ben Langmuir c4436f675d [clang] Use InMemoryModuleCache for readASTFileControlBlock NFC
When a pcm has already been loaded from disk, reuse it from the
InMemoryModuleCache in readASTFileControlBlock. This avoids potentially
reading it again.

As noted in the FIXME, ideally we would also add the module to the cache
if it will be used again later, but that could modify its build state
and we do not have enough context currenlty to know if it's correct.

Differential Revision: https://reviews.llvm.org/D138160
2022-11-17 13:47:46 -08:00
Richard Smith 9e52db1827 When we run out of source locations, try to produce useful information
indicating why we ran out.
2022-11-16 14:36:16 -08:00
Jennifer Yu 628fdc3f57 [OPENMP]Initial support for at clause
Error directive is allowed in both declared and executable contexts.
The function ActOnOpenMPAtClause is called in both places during the
parsers.

Adding a param "bool InExContext" to identify context which is used to
emit error massage.

Differential Revision: https://reviews.llvm.org/D137851
2022-11-15 14:06:50 -08:00
Ben Langmuir 05ec16d90d [clang][deps] Avoid leaking modulemap paths across unrelated imports
Use a FileEntryRef when retrieving modulemap paths in the scanner so
that we use a path compatible with the original module import, rather
than a FileEntry which can allow unrelated modules to leak paths into
how we build a module due to FileManager mutating the path.

Note: the current change prevents an "unrelated" path, but does not
change how VFS mapped paths are handled (which would be calling
getNameAsRequested) nor canonicalize the path.

Differential Revision: https://reviews.llvm.org/D137989
2022-11-15 13:59:26 -08:00
Chuanqi Xu d1f90b6129 [NFC] [C++20] [Modules] Rename ASTWriter::isWritingStdCXXNamedModules
According to the discussion in
https://discourse.llvm.org/t/rfc-unifying-the-terminology-about-modules-in-clang/66054,
this patch rename ASTWriter::isWritingNamedModules to ASTWriter::isWrittingStdCXXNamedModules
to make the name more clear.
2022-11-08 15:58:46 +08:00
Kazu Hirata 94186347a1 [clang] Use llvm::reverse (NFC) 2022-11-06 21:09:20 -08:00
Corentin Jabot 35a870c30a Revert "Implement CWG2631"
This reverts commit 7acfe36294.
This reverts commit 5f87a892a7.
This reverts commit 6875ac6927.
2022-11-04 22:10:50 +01:00
Corentin Jabot 7acfe36294 Implement CWG2631
Implement https://cplusplus.github.io/CWG/issues/2631.html.

Immediate calls in default arguments and defaults members
are not evaluated.

Instead, we evaluate them when constructing a
`CXXDefaultArgExpr`/`BuildCXXDefaultInitExpr`.

The immediate calls are executed by doing a
transform on the initializing expression.

Note that lambdas are not considering subexpressions so
we do not need to transform them.

As a result of this patch, unused default member
initializers are not considered odr-used, and
errors about members binding to local variables
in an outer scope only surface at the point
where a constructor is defined.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D136554
2022-11-04 14:46:38 +01:00
Corentin Jabot 722a0efe31 Revert "Implement CWG2631"
Breaks the build on some platforms.

This reverts commit bf1e235695.
2022-11-04 08:24:52 +01:00
Corentin Jabot bf1e235695 Implement CWG2631
Implement https://cplusplus.github.io/CWG/issues/2631.html.

Immediate calls in default arguments and defaults members
are not evaluated.

Instead, we evaluate them when constructing a
`CXXDefaultArgExpr`/`BuildCXXDefaultInitExpr`.

The immediate calls are executed by doing a
transform on the initializing expression.

Note that lambdas are not considering subexpressions so
we do not need to transform them.

As a result of this patch, unused default member
initializers are not considered odr-used, and
errors about members binding to local variables
in an outer scope only surface at the point
where a constructor is defined.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D136554
2022-11-04 07:58:57 +01:00
Jennifer Yu ea64e66f7b [OPENMP]Initial support for error directive.
Differential Revision: https://reviews.llvm.org/D137209
2022-11-02 14:25:28 -07:00
Jan Svoboda f33173acd6 [clang][modules][deps] System module maps might not be affecting
The dependency scanner relies on the module map filtering logic in `ASTWriter`. The algorithm currently considers all system module maps affecting, which is not only sub-optimal, but can also cause failures when building a module explicitly (see attached test case).

This patch applies the same filtering logic to system module maps.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D136007
2022-11-01 22:41:40 -07:00
Jan Svoboda 6924a49690 [clang][modules] Account for non-affecting inputs in `ASTWriter`
In D106876, we stopped serializing module map files that didn't affect compilation of the current module.

However, since each `SourceLocation` is simply an offset into `SourceManager`'s global buffer of concatenated input files in, these need to be adjusted during serialization. Otherwise, they can incorrectly point after the buffer or into subsequent input file.

This patch starts adjusting `SourceLocation`s, `FileID`s and other `SourceManager` offsets in `ASTWriter`.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D136624
2022-11-01 19:31:51 -07:00
Jan Svoboda a13122c612 [clang][modules] NFCI: Avoid unnecessary serialization logic for non-affecting files
This patch delays some `ASTWriter` logic until after we've checked whether the source location entry we're serializing as an affecting file or not.

Depends on D137214.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D137216
2022-11-01 19:23:02 -07:00
Jan Svoboda 0bfc97e4f4 [clang][modules] NFCI: Scaffolding for serialization of adjusted SourceManager offsets
This patch is a NFC prep for D136624, where we start adjusting offsets into `SourceManager`.

Depends on D137213.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D137214
2022-11-01 19:06:55 -07:00
Jan Svoboda f61c135a69 [clang][modules] NFCI: Pragma diagnostic mappings: write/read FileID instead of SourceLocation
For pragma diagnostic mappings, we always write/read `SourceLocation` with offset 0. This is equivalent to just writing a `FileID`, which is exactly what this patch starts doing.

Depends on D137211.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D137213
2022-11-01 18:59:17 -07:00
Jan Svoboda fdbc55a51a [clang][modules] NFCI: Unify FileID writing/reading
This patch adds new functions for writing/reading `FileID`s and uses them to replace some ad-hoc code.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D137211
2022-11-01 18:57:13 -07:00
Jan Svoboda 6dda497942 [clang][modules] NFC: Remove unused lambda argument 2022-11-01 16:58:11 -07:00
Chuanqi Xu 22914a8229 [NFC] Use isa<...> to replace isa<>||isa<> in clang/Serialization
Now isa supports the variant args, which could simplify the codes
further. This patch simplify the uses in clang/Serialization
2022-10-31 21:17:03 +08:00
Chuanqi Xu e8541e4b42 [NFC] [Modules] Rename modules related things in Preprocessor and AffectingModules
Rename module related things according to the consensus in
https://discourse.llvm.org/t/rfc-unifying-the-terminology-about-modules-in-clang/66054/
to reduce further confusings.

This only renames things I can make sure. It doesn't  mean all the names
in Preprocessor are correct now.
2022-10-27 16:40:26 +08:00
Matheus Izvekov b8064374b2
[clang] Instantiate concepts with sugared template arguments
Since we don't unique specializations for concepts, we can just instantiate
them with the sugared template arguments, at negligible cost.

If we don't track their specializations, we can't resugar them later
anyway, and that would be more expensive than just instantiating them
sugared in the first place since it would require an additional pass.

Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>

Differential Revision: https://reviews.llvm.org/D136566
2022-10-27 06:18:53 +02:00
Matheus Izvekov 31074f5ec2
Revert "[clang] Instantiate concepts with sugared template arguments"
This reverts commit d0a6de59c7.
2022-10-26 10:14:14 +02:00