[clang] [clang][dataflow] Add support for lambda captures (PR #68558)

2023-10-09 Thread Stanislav Gatev via cfe-commits

https://github.com/sgatev created 
https://github.com/llvm/llvm-project/pull/68558

This adds support for copy, ref, and this lambda captures to the core framework 
and also adds relevant test in UncheckedOptionalAccessTest.

>From 85dc5eea1810d5d7af207e211799aa92a0f9f154 Mon Sep 17 00:00:00 2001
From: Stanislav Gatev 
Date: Fri, 6 Oct 2023 04:32:06 +
Subject: [PATCH] [clang][dataflow] Add support for lambda captures

This adds support for copy, ref, and this lambda captures to the core
framework and also adds relevant test in UncheckedOptionalAccessTest.
---
 .../FlowSensitive/DataflowAnalysisContext.h   |   2 +-
 .../FlowSensitive/DataflowEnvironment.h   |   6 +-
 .../FlowSensitive/DataflowAnalysisContext.cpp |   2 +-
 .../FlowSensitive/DataflowEnvironment.cpp |  23 +-
 .../Analysis/FlowSensitive/TestingSupport.cpp |  16 +-
 .../Analysis/FlowSensitive/TestingSupport.h   |  16 +-
 .../Analysis/FlowSensitive/TransferTest.cpp   | 231 ++
 .../UncheckedOptionalAccessModelTest.cpp  | 117 +
 8 files changed, 398 insertions(+), 15 deletions(-)

diff --git 
a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 4698f0616e66e82..c46109a02921e7f 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -98,7 +98,7 @@ class DataflowAnalysisContext {
   StorageLocation &createStorageLocation(QualType Type);
 
   /// Returns a stable storage location for `D`.
-  StorageLocation &getStableStorageLocation(const VarDecl &D);
+  StorageLocation &getStableStorageLocation(const ValueDecl &D);
 
   /// Returns a stable storage location for `E`.
   StorageLocation &getStableStorageLocation(const Expr &E);
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
index 73f747ff88cf447..56d647f35b08430 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
@@ -242,7 +242,7 @@ class Environment {
   /// Creates a storage location for `D`. Does not assign the returned storage
   /// location to `D` in the environment. Does not assign a value to the
   /// returned storage location in the environment.
-  StorageLocation &createStorageLocation(const VarDecl &D);
+  StorageLocation &createStorageLocation(const ValueDecl &D);
 
   /// Creates a storage location for `E`. Does not assign the returned storage
   /// location to `E` in the environment. Does not assign a value to the
@@ -408,7 +408,7 @@ class Environment {
   /// this value. Otherwise, initializes the object with a value created using
   /// `createValue()`.  Uses the storage location returned by
   /// `DataflowAnalysisContext::getStableStorageLocation(D)`.
-  StorageLocation &createObject(const VarDecl &D, const Expr *InitExpr) {
+  StorageLocation &createObject(const ValueDecl &D, const Expr *InitExpr) {
 return createObjectInternal(&D, D.getType(), InitExpr);
   }
 
@@ -614,7 +614,7 @@ class Environment {
 
   /// Shared implementation of `createObject()` overloads.
   /// `D` and `InitExpr` may be null.
-  StorageLocation &createObjectInternal(const VarDecl *D, QualType Ty,
+  StorageLocation &createObjectInternal(const ValueDecl *D, QualType Ty,
 const Expr *InitExpr);
 
   /// Shared implementation of `pushCall` overloads. Note that unlike
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index e81048ce9233808..fa9b40fc49b3ae7 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -73,7 +73,7 @@ StorageLocation 
&DataflowAnalysisContext::createStorageLocation(QualType Type) {
 }
 
 StorageLocation &
-DataflowAnalysisContext::getStableStorageLocation(const VarDecl &D) {
+DataflowAnalysisContext::getStableStorageLocation(const ValueDecl &D) {
   if (auto *Loc = DeclToLoc.lookup(&D))
 return *Loc;
   auto &Loc = createStorageLocation(D.getType().getNonReferenceType());
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 66d98c995468595..d801a54456ea7a5 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -448,11 +448,22 @@ Environment::Environment(DataflowAnalysisContext &DACtx,
   if (const auto *MethodDecl = dyn_cast(&DeclCtx)) {
 auto *Parent = MethodDecl->getParent();
 assert(Parent != nullptr);
-if (Parent->isLambda())
-  MethodDecl = dyn_cast(Parent->getDeclContext());
 
-// FIXME: Initialize the ThisPointeeLoc of lambdas too.
-if (MethodDecl 

[clang] [clang][dataflow] Add support for lambda captures (PR #68558)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

This adds support for copy, ref, and this lambda captures to the core framework 
and also adds relevant test in UncheckedOptionalAccessTest.

---

Patch is 20.50 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/68558.diff


8 Files Affected:

- (modified) 
clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h (+1-1) 
- (modified) clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h 
(+3-3) 
- (modified) clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp 
(+1-1) 
- (modified) clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp (+17-6) 
- (modified) clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp (+14-2) 
- (modified) clang/unittests/Analysis/FlowSensitive/TestingSupport.h (+14-2) 
- (modified) clang/unittests/Analysis/FlowSensitive/TransferTest.cpp (+231) 
- (modified) 
clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp 
(+117) 


``diff
diff --git 
a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 4698f0616e66e82..c46109a02921e7f 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -98,7 +98,7 @@ class DataflowAnalysisContext {
   StorageLocation &createStorageLocation(QualType Type);
 
   /// Returns a stable storage location for `D`.
-  StorageLocation &getStableStorageLocation(const VarDecl &D);
+  StorageLocation &getStableStorageLocation(const ValueDecl &D);
 
   /// Returns a stable storage location for `E`.
   StorageLocation &getStableStorageLocation(const Expr &E);
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
index 73f747ff88cf447..56d647f35b08430 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
@@ -242,7 +242,7 @@ class Environment {
   /// Creates a storage location for `D`. Does not assign the returned storage
   /// location to `D` in the environment. Does not assign a value to the
   /// returned storage location in the environment.
-  StorageLocation &createStorageLocation(const VarDecl &D);
+  StorageLocation &createStorageLocation(const ValueDecl &D);
 
   /// Creates a storage location for `E`. Does not assign the returned storage
   /// location to `E` in the environment. Does not assign a value to the
@@ -408,7 +408,7 @@ class Environment {
   /// this value. Otherwise, initializes the object with a value created using
   /// `createValue()`.  Uses the storage location returned by
   /// `DataflowAnalysisContext::getStableStorageLocation(D)`.
-  StorageLocation &createObject(const VarDecl &D, const Expr *InitExpr) {
+  StorageLocation &createObject(const ValueDecl &D, const Expr *InitExpr) {
 return createObjectInternal(&D, D.getType(), InitExpr);
   }
 
@@ -614,7 +614,7 @@ class Environment {
 
   /// Shared implementation of `createObject()` overloads.
   /// `D` and `InitExpr` may be null.
-  StorageLocation &createObjectInternal(const VarDecl *D, QualType Ty,
+  StorageLocation &createObjectInternal(const ValueDecl *D, QualType Ty,
 const Expr *InitExpr);
 
   /// Shared implementation of `pushCall` overloads. Note that unlike
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index e81048ce9233808..fa9b40fc49b3ae7 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -73,7 +73,7 @@ StorageLocation 
&DataflowAnalysisContext::createStorageLocation(QualType Type) {
 }
 
 StorageLocation &
-DataflowAnalysisContext::getStableStorageLocation(const VarDecl &D) {
+DataflowAnalysisContext::getStableStorageLocation(const ValueDecl &D) {
   if (auto *Loc = DeclToLoc.lookup(&D))
 return *Loc;
   auto &Loc = createStorageLocation(D.getType().getNonReferenceType());
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 66d98c995468595..d801a54456ea7a5 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -448,11 +448,22 @@ Environment::Environment(DataflowAnalysisContext &DACtx,
   if (const auto *MethodDecl = dyn_cast(&DeclCtx)) {
 auto *Parent = MethodDecl->getParent();
 assert(Parent != nullptr);
-if (Parent->isLambda())
-  MethodDecl = dyn_cast(Parent->getDeclContext());
 
-// FIXME: Initialize the ThisPointeeLoc of lambdas too.
-if (MethodDecl && MethodDecl->isImplicitObjectMemberFunction()) {
+if (P

[clang] [clang][dataflow] Add support for lambda captures (PR #68558)

2023-10-09 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff c98bf1e45e22e2a7d107b187f5d3e7abf5e81375 
85dc5eea1810d5d7af207e211799aa92a0f9f154 -- 
clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h 
clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h 
clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp 
clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp 
clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp 
clang/unittests/Analysis/FlowSensitive/TestingSupport.h 
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp 
clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index d801a54456ea..01c6cc69e2b9 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -458,7 +458,8 @@ Environment::Environment(DataflowAnalysisContext &DACtx,
 } else if (Capture.capturesThis()) {
   const auto *SurroundingMethodDecl =
   cast(DeclCtx.getNonClosureAncestor());
-  QualType ThisPointeeType = 
SurroundingMethodDecl->getFunctionObjectParameterType();
+  QualType ThisPointeeType =
+  SurroundingMethodDecl->getFunctionObjectParameterType();
   ThisPointeeLoc =
   &cast(createValue(ThisPointeeType))->getLoc();
 }
diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp 
b/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
index 283bad92457f..65c527ae63d2 100644
--- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
@@ -181,8 +181,7 @@ llvm::Error test::checkDataflowWithNoopAnalysis(
   "-std=" +
   std::string(LangStandard::getLangStandardForKind(Std).getName())};
   AnalysisInputs AI(
-  Code,
-  TargetFuncMatcher,
+  Code, TargetFuncMatcher,
   [UseBuiltinModel = Options.BuiltinOpts.has_value()](ASTContext &C,
   Environment &Env) {
 return NoopAnalysis(

``




https://github.com/llvm/llvm-project/pull/68558
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PowerPC] Fix use of FPSCR builtins in smmintrin.h (PR #67299)

2023-10-09 Thread Qiu Chaofan via cfe-commits


@@ -68,10 +68,10 @@ extern __inline __m128d
 __asm__("mffsce %0" : "=f"(__fpscr_save.__fr));
 __enables_save.__fpscr = __fpscr_save.__fpscr & 0xf8;
 #else
-__fpscr_save.__fr = __builtin_mffs();
+__fpscr_save.__fr = __builtin_ppc_mffs();

ecnelises wrote:

`__builtin_mffs` aliases to `__builtin_ppc_mffs` through macro. But the compat 
macros do not always work. In the test cases using `-ffreestanding` or 
targeting non-AIX non-Linux OSes, the macros will not be defined.

https://github.com/llvm/llvm-project/pull/67299
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154581: [clang][Interp] Track existing InitMaps in InterpState

2023-10-09 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154581/new/

https://reviews.llvm.org/D154581

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157252: [clang][ExprConst] Handle 0 type size in builtin_memcpy etc.

2023-10-09 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157252/new/

https://reviews.llvm.org/D157252

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][dataflow] Add support for lambda captures (PR #68558)

2023-10-09 Thread Stanislav Gatev via cfe-commits

https://github.com/sgatev updated 
https://github.com/llvm/llvm-project/pull/68558

>From 77d474bd7f7885cc454aeb0fdb5b25788d261e41 Mon Sep 17 00:00:00 2001
From: Stanislav Gatev 
Date: Fri, 6 Oct 2023 04:32:06 +
Subject: [PATCH] [clang][dataflow] Add support for lambda captures

This adds support for copy, ref, and this lambda captures to the core
framework and also adds relevant test in UncheckedOptionalAccessTest.
---
 .../FlowSensitive/DataflowAnalysisContext.h   |   2 +-
 .../FlowSensitive/DataflowEnvironment.h   |   6 +-
 .../FlowSensitive/DataflowAnalysisContext.cpp |   2 +-
 .../FlowSensitive/DataflowEnvironment.cpp |  24 +-
 .../Analysis/FlowSensitive/TestingSupport.cpp |  15 +-
 .../Analysis/FlowSensitive/TestingSupport.h   |  16 +-
 .../Analysis/FlowSensitive/TransferTest.cpp   | 231 ++
 .../UncheckedOptionalAccessModelTest.cpp  | 117 +
 8 files changed, 398 insertions(+), 15 deletions(-)

diff --git 
a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 4698f0616e66e82..c46109a02921e7f 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -98,7 +98,7 @@ class DataflowAnalysisContext {
   StorageLocation &createStorageLocation(QualType Type);
 
   /// Returns a stable storage location for `D`.
-  StorageLocation &getStableStorageLocation(const VarDecl &D);
+  StorageLocation &getStableStorageLocation(const ValueDecl &D);
 
   /// Returns a stable storage location for `E`.
   StorageLocation &getStableStorageLocation(const Expr &E);
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h 
b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
index 73f747ff88cf447..56d647f35b08430 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
@@ -242,7 +242,7 @@ class Environment {
   /// Creates a storage location for `D`. Does not assign the returned storage
   /// location to `D` in the environment. Does not assign a value to the
   /// returned storage location in the environment.
-  StorageLocation &createStorageLocation(const VarDecl &D);
+  StorageLocation &createStorageLocation(const ValueDecl &D);
 
   /// Creates a storage location for `E`. Does not assign the returned storage
   /// location to `E` in the environment. Does not assign a value to the
@@ -408,7 +408,7 @@ class Environment {
   /// this value. Otherwise, initializes the object with a value created using
   /// `createValue()`.  Uses the storage location returned by
   /// `DataflowAnalysisContext::getStableStorageLocation(D)`.
-  StorageLocation &createObject(const VarDecl &D, const Expr *InitExpr) {
+  StorageLocation &createObject(const ValueDecl &D, const Expr *InitExpr) {
 return createObjectInternal(&D, D.getType(), InitExpr);
   }
 
@@ -614,7 +614,7 @@ class Environment {
 
   /// Shared implementation of `createObject()` overloads.
   /// `D` and `InitExpr` may be null.
-  StorageLocation &createObjectInternal(const VarDecl *D, QualType Ty,
+  StorageLocation &createObjectInternal(const ValueDecl *D, QualType Ty,
 const Expr *InitExpr);
 
   /// Shared implementation of `pushCall` overloads. Note that unlike
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index e81048ce9233808..fa9b40fc49b3ae7 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -73,7 +73,7 @@ StorageLocation 
&DataflowAnalysisContext::createStorageLocation(QualType Type) {
 }
 
 StorageLocation &
-DataflowAnalysisContext::getStableStorageLocation(const VarDecl &D) {
+DataflowAnalysisContext::getStableStorageLocation(const ValueDecl &D) {
   if (auto *Loc = DeclToLoc.lookup(&D))
 return *Loc;
   auto &Loc = createStorageLocation(D.getType().getNonReferenceType());
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp 
b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 66d98c995468595..01c6cc69e2b9fac 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -448,11 +448,23 @@ Environment::Environment(DataflowAnalysisContext &DACtx,
   if (const auto *MethodDecl = dyn_cast(&DeclCtx)) {
 auto *Parent = MethodDecl->getParent();
 assert(Parent != nullptr);
-if (Parent->isLambda())
-  MethodDecl = dyn_cast(Parent->getDeclContext());
 
-// FIXME: Initialize the ThisPointeeLoc of lambdas too.
-if (MethodDecl && MethodDecl->isImplicitObjectMemberFunction()) {
+if (Parent->isLambda()) {
+  for (auto Capture : Parent->captures()) {
+if

[PATCH] D153131: [clang analysis][thread-safety] Handle return-by-reference...

2023-10-09 Thread Clement Courbet via Phabricator via cfe-commits
courbet added a comment.

In D153131#4653362 , @aaronpuchert 
wrote:

> In D153131#4653345 , @aeubanks 
> wrote:
>
>> This is finding lots of real issues in code, which is awesome, but could I 
>> request that this be put under a separate warning flag so we can toggle off 
>> just the new functionality and turn it on as we clean our codebase? e.g. 
>> `-W[no-]thread-safety-analysis-return`
>
> Fine for me, but we might want to remove it again after one or two releases. 
> I'm not sure how to communicate that this is just a “transitory” flag.
>
> And it should be included by default in `-Wthread-safety-reference`, so that 
> users of that flag see the new warnings/errors, and can demote them to 
> warnings while fixing them. To emphasize the subflag status, I'd suggest 
> something like `-Wthread-safety-reference-return`.

I also had some push back internally on adding this to the existing flag. I'm 
going to add `-Wthread-safety-reference-return`, can we start by not 
temporarily including it in `-Wthread-safety-reference` so that we can see how 
much work it it to fix those warnings ?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153131/new/

https://reviews.llvm.org/D153131

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] d7b18d5 - Use llvm::endianness{,::little,::native} (NFC)

2023-10-09 Thread Kazu Hirata via cfe-commits

Author: Kazu Hirata
Date: 2023-10-09T00:54:47-07:00
New Revision: d7b18d5083648c26b94adc2651edb87848138728

URL: 
https://github.com/llvm/llvm-project/commit/d7b18d5083648c26b94adc2651edb87848138728
DIFF: 
https://github.com/llvm/llvm-project/commit/d7b18d5083648c26b94adc2651edb87848138728.diff

LOG: Use llvm::endianness{,::little,::native} (NFC)

Now that llvm::support::endianness has been renamed to
llvm::endianness, we can use the shorter form.  This patch replaces
llvm::support::endianness with llvm::endianness.

Added: 


Modified: 
clang/include/clang/Basic/ObjCRuntime.h
clang/include/clang/Basic/Sanitizers.h
clang/include/clang/Lex/HeaderSearchOptions.h
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
lld/ELF/Config.h
lldb/unittests/tools/lldb-server/tests/MessageObjects.cpp
lldb/unittests/tools/lldb-server/tests/MessageObjects.h
llvm/include/llvm/DebugInfo/GSYM/FileWriter.h
llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h
llvm/include/llvm/DebugInfo/GSYM/GsymReader.h
llvm/include/llvm/Support/BinaryByteStream.h
llvm/include/llvm/Support/BinaryItemStream.h
llvm/include/llvm/Support/BinaryStream.h
llvm/include/llvm/Support/BinaryStreamReader.h
llvm/include/llvm/Support/BinaryStreamRef.h
llvm/include/llvm/Support/BinaryStreamWriter.h
llvm/include/llvm/Support/VersionTuple.h
llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
llvm/lib/ProfileData/InstrProfReader.cpp
llvm/lib/Support/BinaryStreamRef.cpp
llvm/lib/Support/BinaryStreamWriter.cpp
llvm/lib/Target/ARM/Disassembler/ARMDisassembler.cpp
llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
llvm/tools/llvm-objdump/llvm-objdump.cpp
llvm/unittests/ADT/HashingTest.cpp
llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp
llvm/unittests/Support/HashBuilderTest.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/ObjCRuntime.h 
b/clang/include/clang/Basic/ObjCRuntime.h
index d783154c3f9f135..500b2462f007736 100644
--- a/clang/include/clang/Basic/ObjCRuntime.h
+++ b/clang/include/clang/Basic/ObjCRuntime.h
@@ -482,7 +482,7 @@ class ObjCRuntime {
 return llvm::hash_combine(OCR.getKind(), OCR.getVersion());
   }
 
-  template 
+  template 
   friend void addHash(llvm::HashBuilder &HBuilder,
   const ObjCRuntime &OCR) {
 HBuilder.add(OCR.getKind(), OCR.getVersion());

diff  --git a/clang/include/clang/Basic/Sanitizers.h 
b/clang/include/clang/Basic/Sanitizers.h
index 090a3a7fa907625..8fdaf2e4ba8ab18 100644
--- a/clang/include/clang/Basic/Sanitizers.h
+++ b/clang/include/clang/Basic/Sanitizers.h
@@ -77,7 +77,7 @@ class SanitizerMask {
 
   llvm::hash_code hash_value() const;
 
-  template 
+  template 
   friend void addHash(llvm::HashBuilder &HBuilder,
   const SanitizerMask &SM) {
 HBuilder.addRange(&SM.maskLoToHigh[0], &SM.maskLoToHigh[kNumElem]);

diff  --git a/clang/include/clang/Lex/HeaderSearchOptions.h 
b/clang/include/clang/Lex/HeaderSearchOptions.h
index 206bc69d7b2cdcb..c7d95006bb779ad 100644
--- a/clang/include/clang/Lex/HeaderSearchOptions.h
+++ b/clang/include/clang/Lex/HeaderSearchOptions.h
@@ -267,7 +267,7 @@ inline llvm::hash_code hash_value(const 
HeaderSearchOptions::Entry &E) {
   return llvm::hash_combine(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
 }
 
-template 
+template 
 inline void addHash(llvm::HashBuilder &HBuilder,
 const HeaderSearchOptions::Entry &E) {
   HBuilder.add(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
@@ -278,7 +278,7 @@ hash_value(const HeaderSearchOptions::SystemHeaderPrefix 
&SHP) {
   return llvm::hash_combine(SHP.Prefix, SHP.IsSystemHeader);
 }
 
-template 
+template 
 inline void addHash(llvm::HashBuilder &HBuilder,
 const HeaderSearchOptions::SystemHeaderPrefix &SHP) {
   HBuilder.add(SHP.Prefix, SHP.IsSystemHeader);

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index f2fe9046a6c1fe2..bb442495f58359c 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4630,7 +4630,7 @@ bool 
CompilerInvocation::CreateFromArgs(CompilerInvocation &Invocation,
 
 std::string CompilerInvocation::getModuleHash() const {
   // FIXME: Consider using SHA1 instead of MD5.
-  llvm::HashBuilder HBuilder;
+  llvm::HashBuilder HBuilder;
 
   // Note: For QoI reasons, the things we use as a hash here should all be
   // dumped via the -module-info flag.

diff  --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp 
b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index 5e028c4431fe90f..40115b7b5ae25b3 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepC

[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread Endre Fülöp via cfe-commits

https://github.com/gamesh411 updated 
https://github.com/llvm/llvm-project/pull/67663

From 9aea93ddeb70245a07984188aa98577d54e8e560 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= 
Date: Fri, 8 Sep 2023 14:20:00 +0200
Subject: [PATCH 1/9] [analyzer][clangsa] Add new option to
 alpha.security.cert.InvalidPtrChecker

The invalidation of pointer pointers returned by subsequent calls to genenv is
suggested by the POSIX standard, but is too strict from a practical point of
view. A new checker option 'InvalidatingGetEnv' is introduced, and is set to a
more lax default value, which does not consider consecutive getenv calls
invalidating.
The handling of the main function's possible specification where an environment
pointer is also pecified as a third parameter is also considered now.

Differential Revision: https://reviews.llvm.org/D154603
---
 .../clang/StaticAnalyzer/Checkers/Checkers.td |  9 ++
 .../Checkers/cert/InvalidPtrChecker.cpp   | 86 ++-
 clang/test/Analysis/analyzer-config.c |  1 +
 .../Analysis/cert/env34-c-cert-examples.c | 40 -
 clang/test/Analysis/cert/env34-c.c|  1 +
 clang/test/Analysis/invalid-ptr-checker.c | 50 +++
 6 files changed, 163 insertions(+), 24 deletions(-)
 create mode 100644 clang/test/Analysis/invalid-ptr-checker.c

diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td 
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index 65c1595eb6245dd..b4f65c934bf483b 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -997,6 +997,15 @@ let ParentPackage = ENV in {
 
   def InvalidPtrChecker : Checker<"InvalidPtr">,
   HelpText<"Finds usages of possibly invalidated pointers">,
+  CheckerOptions<[
+CmdLineOption,
+  ]>,
   Documentation;
 
 } // end "alpha.cert.env"
diff --git a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
index aae1a17bc0ae53e..8849eb1148564b7 100644
--- a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
@@ -38,6 +38,15 @@ class InvalidPtrChecker
 CheckerContext &C) const;
 
   // SEI CERT ENV31-C
+
+  // If set to true, consider getenv calls as invalidating operations on the
+  // environment variable buffer. This is implied in the standard, but in
+  // practice does not cause problems (in the commonly used environments).
+  bool InvalidatingGetEnv = false;
+
+  // GetEnv can be treated invalidating and non-invalidating as well.
+  const CallDescription GetEnvCall{{"getenv"}, 1};
+
   const CallDescriptionMap EnvpInvalidatingFunctions = {
   {{{"setenv"}, 3}, &InvalidPtrChecker::EnvpInvalidatingCall},
   {{{"unsetenv"}, 1}, &InvalidPtrChecker::EnvpInvalidatingCall},
@@ -51,7 +60,6 @@ class InvalidPtrChecker
 
   // SEI CERT ENV34-C
   const CallDescriptionMap PreviousCallInvalidatingFunctions = {
-  {{{"getenv"}, 1}, 
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   {{{"setlocale"}, 2},
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   {{{"strerror"}, 1},
@@ -62,6 +70,10 @@ class InvalidPtrChecker
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   };
 
+  // The private members of this checker corresponding to commandline options
+  // are set in this function.
+  friend void ento::registerInvalidPtrChecker(CheckerManager &);
+
 public:
   // Obtain the environment pointer from 'main()' (if present).
   void checkBeginFunction(CheckerContext &C) const;
@@ -84,7 +96,10 @@ class InvalidPtrChecker
 REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)
 
 // Stores the region of the environment pointer of 'main' (if present).
-REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const MemRegion *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(MainEnvPtrRegion, const MemRegion *)
+
+// Stores the regions of environments returned by getenv calls.
+REGISTER_SET_WITH_PROGRAMSTATE(GetenvEnvPtrRegions, const MemRegion *)
 
 // Stores key-value pairs, where key is function declaration and value is
 // pointer to memory region returned by previous call of this function
@@ -95,22 +110,35 @@ void InvalidPtrChecker::EnvpInvalidatingCall(const 
CallEvent &Call,
  CheckerContext &C) const {
   StringRef FunctionName = Call.getCalleeIdentifier()->getName();
   ProgramStateRef State = C.getState();
-  const MemRegion *SymbolicEnvPtrRegion = State->get();
-  if (!SymbolicEnvPtrRegion)
-return;
 
-  State = State->add(SymbolicEnvPtrRegion);
+  auto PlaceInvalidationNote = [&C, FunctionName,
+&State](const MemRegion *Region,
+StringRef Message, ExplodedNode *Pred) 
{
+State = State->add(Region);
+
+// Make cop

[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

2023-10-09 Thread via cfe-commits

https://github.com/DaMatrix updated 
https://github.com/llvm/llvm-project/pull/66894

>From f05f52ed4e74cf857bf07aef5dbd5d0861591a14 Mon Sep 17 00:00:00 2001
From: DaPorkchop_ 
Date: Sun, 13 Aug 2023 22:39:12 +0200
Subject: [PATCH] [clang] Implement constexpr bit_cast for vectors

---
 .../include/clang/Basic/DiagnosticASTKinds.td |   3 +
 clang/lib/AST/ExprConstant.cpp| 247 +++---
 .../SemaCXX/constexpr-builtin-bit-cast.cpp|  61 +
 3 files changed, 217 insertions(+), 94 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td 
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index d2656310e79c9b8..3f06e18783dd558 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -326,6 +326,9 @@ def note_constexpr_bit_cast_invalid_type : Note<
   "%select{type|member}1 is not allowed in a constant expression">;
 def note_constexpr_bit_cast_invalid_subtype : Note<
   "invalid type %0 is a %select{member|base}1 of %2">;
+def note_constexpr_bit_cast_invalid_vector : Note<
+  "bit_cast involving type %0 is not allowed in a constant expression; "
+  "element size %1 * element count %2 is not a multiple of the byte size %3">;
 def note_constexpr_bit_cast_indet_dest : Note<
   "indeterminate value can only initialize an object of type 'unsigned char'"
   "%select{, 'char',|}1 or 'std::byte'; %0 is invalid">;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index a142ea7c47a4730..ad4d1dd12edb2e8 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2732,53 +2732,6 @@ static bool truncateBitfieldValue(EvalInfo &Info, const 
Expr *E,
   return true;
 }
 
-static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
-  llvm::APInt &Res) {
-  APValue SVal;
-  if (!Evaluate(SVal, Info, E))
-return false;
-  if (SVal.isInt()) {
-Res = SVal.getInt();
-return true;
-  }
-  if (SVal.isFloat()) {
-Res = SVal.getFloat().bitcastToAPInt();
-return true;
-  }
-  if (SVal.isVector()) {
-QualType VecTy = E->getType();
-unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
-QualType EltTy = VecTy->castAs()->getElementType();
-unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
-bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
-Res = llvm::APInt::getZero(VecSize);
-for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
-  APValue &Elt = SVal.getVectorElt(i);
-  llvm::APInt EltAsInt;
-  if (Elt.isInt()) {
-EltAsInt = Elt.getInt();
-  } else if (Elt.isFloat()) {
-EltAsInt = Elt.getFloat().bitcastToAPInt();
-  } else {
-// Don't try to handle vectors of anything other than int or float
-// (not sure if it's possible to hit this case).
-Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
-return false;
-  }
-  unsigned BaseEltSize = EltAsInt.getBitWidth();
-  if (BigEndian)
-Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
-  else
-Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
-}
-return true;
-  }
-  // Give up if the input isn't an int, float, or vector.  For example, we
-  // reject "(v4i16)(intptr_t)&a".
-  Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
-  return false;
-}
-
 /// Perform the given integer operation, which is known to need at most 
BitWidth
 /// bits, and check for overflow in the original type (if that type was not an
 /// unsigned type).
@@ -7008,10 +6961,11 @@ class APValueToBufferConverter {
   return visitArray(Val, Ty, Offset);
 case APValue::Struct:
   return visitRecord(Val, Ty, Offset);
+case APValue::Vector:
+  return visitVector(Val, Ty, Offset);
 
 case APValue::ComplexInt:
 case APValue::ComplexFloat:
-case APValue::Vector:
 case APValue::FixedPoint:
   // FIXME: We should support these.
 
@@ -7098,6 +7052,61 @@ class APValueToBufferConverter {
 return true;
   }
 
+  bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
+const VectorType *VTy = Ty->castAs();
+QualType EltTy = VTy->getElementType();
+unsigned NElts = VTy->getNumElements();
+unsigned EltSize =
+VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(EltTy);
+
+if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) {
+  // The vector's size in bits is not a multiple of the target's byte size,
+  // so its layout is unspecified. For now, we'll simply treat these cases
+  // as unsupported (this should only be possible with OpenCL bool vectors
+  // whose element count isn't a multiple of the byte size).
+  Info.FFDiag(BCE->getBeginLoc(),
+  diag::note_constexpr_bit_cast_invalid_vector)
+  << Ty.getCanonicalType() << EltSize << NElts
+  << Info.Ctx.getCharWidth();
+  return false;
+}
+
+if (VTy->isExtV

[clang] 42c564d - [clang-format][NFC] Make InsertNewlineAtEOF a little more efficient

2023-10-09 Thread Owen Pan via cfe-commits

Author: Owen Pan
Date: 2023-10-09T01:09:45-07:00
New Revision: 42c564df2f479efb38e6f02340e370815b439eb1

URL: 
https://github.com/llvm/llvm-project/commit/42c564df2f479efb38e6f02340e370815b439eb1
DIFF: 
https://github.com/llvm/llvm-project/commit/42c564df2f479efb38e6f02340e370815b439eb1.diff

LOG: [clang-format][NFC] Make InsertNewlineAtEOF a little more efficient

Added: 


Modified: 
clang/lib/Format/TokenAnnotator.cpp

Removed: 




diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 6f879006465ecf5..543c119620bf28f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1414,10 +1414,6 @@ class AnnotatingParser {
 Tok->setType(TT_TrailingReturnArrow);
   }
   break;
-case tok::eof:
-  if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0)
-Tok->NewlinesBefore = 1;
-  break;
 default:
   break;
 }
@@ -3244,8 +3240,14 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
   else if (Line.startsWith(TT_ObjCProperty))
 Line.Type = LT_ObjCProperty;
 
-  Line.First->SpacesRequiredBefore = 1;
-  Line.First->CanBreakBefore = Line.First->MustBreakBefore;
+  auto *First = Line.First;
+  First->SpacesRequiredBefore = 1;
+  First->CanBreakBefore = First->MustBreakBefore;
+
+  if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
+  Style.InsertNewlineAtEOF) {
+First->NewlinesBefore = 1;
+  }
 }
 
 // This function heuristically determines whether 'Current' starts the name of 
a



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread Endre Fülöp via cfe-commits

https://github.com/gamesh411 updated 
https://github.com/llvm/llvm-project/pull/67663

From 9aea93ddeb70245a07984188aa98577d54e8e560 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= 
Date: Fri, 8 Sep 2023 14:20:00 +0200
Subject: [PATCH 01/10] [analyzer][clangsa] Add new option to
 alpha.security.cert.InvalidPtrChecker

The invalidation of pointer pointers returned by subsequent calls to genenv is
suggested by the POSIX standard, but is too strict from a practical point of
view. A new checker option 'InvalidatingGetEnv' is introduced, and is set to a
more lax default value, which does not consider consecutive getenv calls
invalidating.
The handling of the main function's possible specification where an environment
pointer is also pecified as a third parameter is also considered now.

Differential Revision: https://reviews.llvm.org/D154603
---
 .../clang/StaticAnalyzer/Checkers/Checkers.td |  9 ++
 .../Checkers/cert/InvalidPtrChecker.cpp   | 86 ++-
 clang/test/Analysis/analyzer-config.c |  1 +
 .../Analysis/cert/env34-c-cert-examples.c | 40 -
 clang/test/Analysis/cert/env34-c.c|  1 +
 clang/test/Analysis/invalid-ptr-checker.c | 50 +++
 6 files changed, 163 insertions(+), 24 deletions(-)
 create mode 100644 clang/test/Analysis/invalid-ptr-checker.c

diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td 
b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index 65c1595eb6245dd..b4f65c934bf483b 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -997,6 +997,15 @@ let ParentPackage = ENV in {
 
   def InvalidPtrChecker : Checker<"InvalidPtr">,
   HelpText<"Finds usages of possibly invalidated pointers">,
+  CheckerOptions<[
+CmdLineOption,
+  ]>,
   Documentation;
 
 } // end "alpha.cert.env"
diff --git a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
index aae1a17bc0ae53e..8849eb1148564b7 100644
--- a/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
@@ -38,6 +38,15 @@ class InvalidPtrChecker
 CheckerContext &C) const;
 
   // SEI CERT ENV31-C
+
+  // If set to true, consider getenv calls as invalidating operations on the
+  // environment variable buffer. This is implied in the standard, but in
+  // practice does not cause problems (in the commonly used environments).
+  bool InvalidatingGetEnv = false;
+
+  // GetEnv can be treated invalidating and non-invalidating as well.
+  const CallDescription GetEnvCall{{"getenv"}, 1};
+
   const CallDescriptionMap EnvpInvalidatingFunctions = {
   {{{"setenv"}, 3}, &InvalidPtrChecker::EnvpInvalidatingCall},
   {{{"unsetenv"}, 1}, &InvalidPtrChecker::EnvpInvalidatingCall},
@@ -51,7 +60,6 @@ class InvalidPtrChecker
 
   // SEI CERT ENV34-C
   const CallDescriptionMap PreviousCallInvalidatingFunctions = {
-  {{{"getenv"}, 1}, 
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   {{{"setlocale"}, 2},
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   {{{"strerror"}, 1},
@@ -62,6 +70,10 @@ class InvalidPtrChecker
&InvalidPtrChecker::postPreviousReturnInvalidatingCall},
   };
 
+  // The private members of this checker corresponding to commandline options
+  // are set in this function.
+  friend void ento::registerInvalidPtrChecker(CheckerManager &);
+
 public:
   // Obtain the environment pointer from 'main()' (if present).
   void checkBeginFunction(CheckerContext &C) const;
@@ -84,7 +96,10 @@ class InvalidPtrChecker
 REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)
 
 // Stores the region of the environment pointer of 'main' (if present).
-REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const MemRegion *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(MainEnvPtrRegion, const MemRegion *)
+
+// Stores the regions of environments returned by getenv calls.
+REGISTER_SET_WITH_PROGRAMSTATE(GetenvEnvPtrRegions, const MemRegion *)
 
 // Stores key-value pairs, where key is function declaration and value is
 // pointer to memory region returned by previous call of this function
@@ -95,22 +110,35 @@ void InvalidPtrChecker::EnvpInvalidatingCall(const 
CallEvent &Call,
  CheckerContext &C) const {
   StringRef FunctionName = Call.getCalleeIdentifier()->getName();
   ProgramStateRef State = C.getState();
-  const MemRegion *SymbolicEnvPtrRegion = State->get();
-  if (!SymbolicEnvPtrRegion)
-return;
 
-  State = State->add(SymbolicEnvPtrRegion);
+  auto PlaceInvalidationNote = [&C, FunctionName,
+&State](const MemRegion *Region,
+StringRef Message, ExplodedNode *Pred) 
{
+State = State->add(Region);
+
+// Make c

[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread Endre Fülöp via cfe-commits


@@ -94,23 +119,40 @@ REGISTER_MAP_WITH_PROGRAMSTATE(PreviousCallResultMap, 
const FunctionDecl *,
 void InvalidPtrChecker::EnvpInvalidatingCall(const CallEvent &Call,
  CheckerContext &C) const {
   StringRef FunctionName = Call.getCalleeIdentifier()->getName();
-  ProgramStateRef State = C.getState();
-  const MemRegion *SymbolicEnvPtrRegion = State->get();
-  if (!SymbolicEnvPtrRegion)
-return;
-
-  State = State->add(SymbolicEnvPtrRegion);
 
-  const NoteTag *Note =
-  C.getNoteTag([SymbolicEnvPtrRegion, FunctionName](
-   PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
-if (!BR.isInteresting(SymbolicEnvPtrRegion))
-  return;
-Out << '\'' << FunctionName
-<< "' call may invalidate the environment parameter of 'main'";
-  });
+  auto PlaceInvalidationNote = [&C, FunctionName](ProgramStateRef State,
+  const MemRegion *Region,
+  StringRef Message,
+  ExplodedNode *Pred) {
+State = State->add(Region);
+
+// Make copy of string data for the time when notes are *actually* created.
+const NoteTag *Note =
+C.getNoteTag([Region, FunctionName = std::string{FunctionName},
+  Message = std::string{Message}](
+ PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
+  if (!BR.isInteresting(Region) ||
+  &BR.getBugType() != InvalidPtrBugType)
+return;
+  Out << '\'' << FunctionName << "' " << Message;
+  BR.markNotInteresting(Region);
+});
+return C.addTransition(State, Pred, Note);
+  };

gamesh411 wrote:

refactored this part, see 
https://github.com/llvm/llvm-project/pull/67663/commits/1faca072459898c26d7e19b2ba1fe1315b9e2171

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread Endre Fülöp via cfe-commits


@@ -1,15 +1,49 @@
+// Default options.
 // RUN: %clang_analyze_cc1 \
 // RUN:  -analyzer-checker=core,alpha.security.cert.env.InvalidPtr \
 // RUN:  -verify -Wno-unused %s
+//
+// Test the laxer handling of getenv function (this is the default).
+// RUN: %clang_analyze_cc1 \
+// RUN:  -analyzer-checker=core,alpha.security.cert.env.InvalidPtr \
+// RUN:  -analyzer-config 
alpha.security.cert.env.InvalidPtr:InvalidatingGetEnv=false \
+// RUN:  -verify -Wno-unused %s
+//
+// Test the stricter handling of getenv function.
+// RUN: %clang_analyze_cc1 \
+// RUN:  -analyzer-checker=core,alpha.security.cert.env.InvalidPtr \
+// RUN:  -analyzer-config 
alpha.security.cert.env.InvalidPtr:InvalidatingGetEnv=true \
+// RUN:  -verify=pedantic -Wno-unused %s

gamesh411 wrote:

this is addressed in 
https://github.com/llvm/llvm-project/pull/67663/commits/39ed2178944539aaa8e221ac11a8d6c7ec93675e

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread Endre Fülöp via cfe-commits


@@ -94,23 +119,40 @@ REGISTER_MAP_WITH_PROGRAMSTATE(PreviousCallResultMap, 
const FunctionDecl *,
 void InvalidPtrChecker::EnvpInvalidatingCall(const CallEvent &Call,
  CheckerContext &C) const {
   StringRef FunctionName = Call.getCalleeIdentifier()->getName();
-  ProgramStateRef State = C.getState();
-  const MemRegion *SymbolicEnvPtrRegion = State->get();
-  if (!SymbolicEnvPtrRegion)
-return;
-
-  State = State->add(SymbolicEnvPtrRegion);
 
-  const NoteTag *Note =
-  C.getNoteTag([SymbolicEnvPtrRegion, FunctionName](
-   PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
-if (!BR.isInteresting(SymbolicEnvPtrRegion))
-  return;
-Out << '\'' << FunctionName
-<< "' call may invalidate the environment parameter of 'main'";
-  });
+  auto PlaceInvalidationNote = [&C, FunctionName](ProgramStateRef State,
+  const MemRegion *Region,
+  StringRef Message,
+  ExplodedNode *Pred) {
+State = State->add(Region);
+
+// Make copy of string data for the time when notes are *actually* created.
+const NoteTag *Note =
+C.getNoteTag([Region, FunctionName = std::string{FunctionName},
+  Message = std::string{Message}](
+ PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
+  if (!BR.isInteresting(Region) ||
+  &BR.getBugType() != InvalidPtrBugType)
+return;
+  Out << '\'' << FunctionName << "' " << Message;
+  BR.markNotInteresting(Region);
+});
+return C.addTransition(State, Pred, Note);
+  };
 
-  C.addTransition(State, Note);
+  ProgramStateRef State = C.getState();
+  ExplodedNode *CurrentChainEnd = C.getPredecessor();
+
+  if (const MemRegion *MainEnvPtr = State->get())
+CurrentChainEnd = PlaceInvalidationNote(
+State, MainEnvPtr,
+"call may invalidate the environment parameter of 'main'",
+CurrentChainEnd);
+
+  for (const MemRegion *EnvPtr : State->get())
+CurrentChainEnd = PlaceInvalidationNote(
+State, EnvPtr, "call may invalidate the environment returned by 
getenv",
+CurrentChainEnd);

gamesh411 wrote:

I have a test now, and could indeed verify, that without marking the 
invalidation regions *not* interesting, this test fails
https://github.com/llvm/llvm-project/pull/67663/commits/d15e570f37f6fb321daf3742231408585f577137

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [mlir] Add DenseUI32ArrayAttr (PR #68230)

2023-10-09 Thread Maya Amrami via cfe-commits

https://github.com/amrami updated 
https://github.com/llvm/llvm-project/pull/68230

>From 0b4f0d2c6c0aac64343e78f95b1bf6b30f63b3ae Mon Sep 17 00:00:00 2001
From: Maya Amrami 
Date: Thu, 21 Sep 2023 18:11:50 +0300
Subject: [PATCH] [mlir] Add DenseUI32ArrayAttr

---
 mlir/include/mlir/IR/Builders.h   |  1 +
 mlir/include/mlir/IR/BuiltinAttributes.h  |  2 ++
 mlir/include/mlir/IR/CommonAttrConstraints.td |  1 +
 mlir/lib/IR/Builders.cpp  |  4 
 mlir/lib/IR/BuiltinAttributes.cpp |  4 
 mlir/test/IR/attribute.mlir   | 13 -
 mlir/test/lib/Dialect/Test/TestOps.td |  9 +++--
 7 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h
index 3eca8bd12f43364..71d761821baeb73 100644
--- a/mlir/include/mlir/IR/Builders.h
+++ b/mlir/include/mlir/IR/Builders.h
@@ -154,6 +154,7 @@ class Builder {
   DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef values);
   DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef values);
   DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef values);
+  DenseUI32ArrayAttr getDenseUI32ArrayAttr(ArrayRef values);
   DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef values);
   DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef values);
   DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef values);
diff --git a/mlir/include/mlir/IR/BuiltinAttributes.h 
b/mlir/include/mlir/IR/BuiltinAttributes.h
index c8161604aad3503..ee12e9c59986eb0 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.h
+++ b/mlir/include/mlir/IR/BuiltinAttributes.h
@@ -755,6 +755,7 @@ class DenseArrayAttrImpl : public DenseArrayAttr {
 extern template class DenseArrayAttrImpl;
 extern template class DenseArrayAttrImpl;
 extern template class DenseArrayAttrImpl;
+extern template class DenseArrayAttrImpl;
 extern template class DenseArrayAttrImpl;
 extern template class DenseArrayAttrImpl;
 extern template class DenseArrayAttrImpl;
@@ -765,6 +766,7 @@ extern template class DenseArrayAttrImpl;
 using DenseBoolArrayAttr = detail::DenseArrayAttrImpl;
 using DenseI8ArrayAttr = detail::DenseArrayAttrImpl;
 using DenseI16ArrayAttr = detail::DenseArrayAttrImpl;
+using DenseUI32ArrayAttr = detail::DenseArrayAttrImpl;
 using DenseI32ArrayAttr = detail::DenseArrayAttrImpl;
 using DenseI64ArrayAttr = detail::DenseArrayAttrImpl;
 using DenseF32ArrayAttr = detail::DenseArrayAttrImpl;
diff --git a/mlir/include/mlir/IR/CommonAttrConstraints.td 
b/mlir/include/mlir/IR/CommonAttrConstraints.td
index 0312ac7ec1d8df5..6bdf286d0928913 100644
--- a/mlir/include/mlir/IR/CommonAttrConstraints.td
+++ b/mlir/include/mlir/IR/CommonAttrConstraints.td
@@ -433,6 +433,7 @@ class DenseArrayAttrBase;
 def DenseI8ArrayAttr : DenseArrayAttrBase<"DenseI8ArrayAttr", "int8_t", "i8">;
 def DenseI16ArrayAttr : DenseArrayAttrBase<"DenseI16ArrayAttr", "int16_t", 
"i16">;
+def DenseUI32ArrayAttr : DenseArrayAttrBase<"DenseUI32ArrayAttr", "uint32_t", 
"ui32">;
 def DenseI32ArrayAttr : DenseArrayAttrBase<"DenseI32ArrayAttr", "int32_t", 
"i32">;
 def DenseI64ArrayAttr : DenseArrayAttrBase<"DenseI64ArrayAttr", "int64_t", 
"i64">;
 def DenseF32ArrayAttr : DenseArrayAttrBase<"DenseF32ArrayAttr", "float", 
"f32">;
diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp
index ab20f4863e11c23..ba08544eb4059a0 100644
--- a/mlir/lib/IR/Builders.cpp
+++ b/mlir/lib/IR/Builders.cpp
@@ -180,6 +180,10 @@ DenseI32ArrayAttr 
Builder::getDenseI32ArrayAttr(ArrayRef values) {
   return DenseI32ArrayAttr::get(context, values);
 }
 
+DenseUI32ArrayAttr Builder::getDenseUI32ArrayAttr(ArrayRef values) {
+  return DenseUI32ArrayAttr::get(context, values);
+}
+
 DenseI64ArrayAttr Builder::getDenseI64ArrayAttr(ArrayRef values) {
   return DenseI64ArrayAttr::get(context, values);
 }
diff --git a/mlir/lib/IR/BuiltinAttributes.cpp 
b/mlir/lib/IR/BuiltinAttributes.cpp
index 5f1129326f4f772..5222c5801d347c0 100644
--- a/mlir/lib/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/IR/BuiltinAttributes.cpp
@@ -761,6 +761,9 @@ struct DenseArrayAttrUtil : public 
DenseArrayAttrIntUtil<8> {
 template <>
 struct DenseArrayAttrUtil : public DenseArrayAttrIntUtil<16> {};
 template <>
+struct DenseArrayAttrUtil
+: public DenseArrayAttrIntUtil<32, IntegerType::Unsigned> {};
+template <>
 struct DenseArrayAttrUtil : public DenseArrayAttrIntUtil<32> {};
 template <>
 struct DenseArrayAttrUtil : public DenseArrayAttrIntUtil<64> {};
@@ -876,6 +879,7 @@ namespace detail {
 template class DenseArrayAttrImpl;
 template class DenseArrayAttrImpl;
 template class DenseArrayAttrImpl;
+template class DenseArrayAttrImpl;
 template class DenseArrayAttrImpl;
 template class DenseArrayAttrImpl;
 template class DenseArrayAttrImpl;
diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir
index 291d5832fce79aa..05ddb39b75eb658 100644
--- a/mlir/test/IR/attribute.mlir
+++ b/mlir/test/IR/attribute.mlir
@@ -592,6 +592,8 @@ func.func @dense_array_attr() attributes {
 

[clang] [AArch64][SME] Remove immediate argument restriction for svldr and svstr (PR #68565)

2023-10-09 Thread Sam Tebbs via cfe-commits

https://github.com/SamTebbs33 created 
https://github.com/llvm/llvm-project/pull/68565

The svldr_vnum_za and svstr_vnum_za builtins/intrinsics currently require that 
the vnum argument be an immediate, since the instructions take an immediate 
vector number. However, we emit 0 as the immediate for the instruction no 
matter what, and instead modify the base register.

This patch removes that restriction on the argument, so that the argument can 
be a non-immediate. If an appropriate immediate was passed to the builtin then 
CGBuiltin passes that directly to the LLVM intrinsic, otherwise it modifies the 
base register as is existing behaviour.

>From f57f952989ee64d419dc51e9ecf9786720ece3ff Mon Sep 17 00:00:00 2001
From: Samuel Tebbs 
Date: Fri, 6 Oct 2023 17:09:36 +0100
Subject: [PATCH] [AArch64][SME] Remove immediate argument restriction for
 svldr and svstr

The svldr_vnum_za and svstr_vnum_za builtins/intrinsics currently
require that the vnum argument be an immediate, since the instructions
take an immediate vector number. However, we emit 0 as the immediate
for the instruction no matter what, and instead modify the base register.

This patch removes that restriction on the argument, so that the
argument can be a non-immediate. If an appropriate immediate was
passed to the builtin then CGBuiltin passes that directly to the LLVM
intrinsic, otherwise it modifies the base register as is existing
behaviour.
---
 clang/include/clang/Basic/arm_sme.td  |  5 +-
 clang/lib/CodeGen/CGBuiltin.cpp   | 42 +
 .../aarch64-sme-intrinsics/acle_sme_ldr.c | 26 +---
 .../aarch64-sme-intrinsics/acle_sme_str.c | 19 +++---
 .../aarch64-sme-intrinsics/acle_sme_imm.cpp   |  8 ---
 llvm/include/llvm/IR/IntrinsicsAArch64.td |  4 +-
 llvm/lib/Target/AArch64/SMEInstrFormats.td| 10 ++--
 .../CostModel/ARM/unaligned_double_load.ll| 59 +++
 .../CodeGen/AArch64/sme-intrinsics-loads.ll   | 33 ---
 9 files changed, 150 insertions(+), 56 deletions(-)
 create mode 100644 llvm/test/Analysis/CostModel/ARM/unaligned_double_load.ll

diff --git a/clang/include/clang/Basic/arm_sme.td 
b/clang/include/clang/Basic/arm_sme.td
index d014900d719c338..49ef6b6b3fc4359 100644
--- a/clang/include/clang/Basic/arm_sme.td
+++ b/clang/include/clang/Basic/arm_sme.td
@@ -44,10 +44,9 @@ defm SVLD1_ZA32 : ZALoad<"za32", "i", "aarch64_sme_ld1w", 
[ImmCheck<0, ImmCheck0
 defm SVLD1_ZA64 : ZALoad<"za64", "l", "aarch64_sme_ld1d", [ImmCheck<0, 
ImmCheck0_7>]>;
 defm SVLD1_ZA128 : ZALoad<"za128", "q", "aarch64_sme_ld1q", [ImmCheck<0, 
ImmCheck0_15>]>;
 
-def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQi", "",
+def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQn", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
-  MemEltTyDefault, "aarch64_sme_ldr",
-  [ImmCheck<2, ImmCheck0_15>]>;
+  MemEltTyDefault, "aarch64_sme_ldr">;
 
 def SVLDR_ZA : MInst<"svldr_za", "vmQ", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d14cf0dccb09982..ca4bf498cab9535 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -9606,7 +9606,7 @@ Value *CodeGenFunction::EmitSVEMaskedStore(const CallExpr 
*E,
 }
 
 Value *CodeGenFunction::EmitTileslice(Value *Offset, Value *Base) {
-  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int32Ty, false);
+  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int64Ty, false);
   return Builder.CreateAdd(Base, CastOffset, "tileslice");
 }
 
@@ -9665,18 +9665,34 @@ Value *CodeGenFunction::EmitSMEZero(const SVETypeFlags 
&TypeFlags,
 Value *CodeGenFunction::EmitSMELdrStr(const SVETypeFlags &TypeFlags,
   SmallVectorImpl &Ops,
   unsigned IntID) {
-  if (Ops.size() == 3) {
-Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
-llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
-llvm::Value *MulVL = Builder.CreateMul(
-CntsbCall,
-Builder.getInt64(cast(Ops[2])->getZExtValue()),
-"mulvl");
-
-Ops[1] = Builder.CreateGEP(Int8Ty, Ops[1], MulVL);
-Ops[0] = EmitTileslice(Ops[0], Ops[2]);
-Ops.erase(&Ops[2]);
-  }
+  if (Ops.size() == 2) {
+// Intrinsics without a vecnum also use this function, so just provide 0
+Ops.push_back(Ops[1]);
+Ops[1] = Builder.getInt32(0);
+  } else {
+int Imm = -1;
+if (ConstantInt* C = dyn_cast(Ops[2]))
+  if (C->getZExtValue() <= 15)
+  Imm = C->getZExtValue();
+
+if (Imm != -1) {
+  Ops[2] = Ops[1];
+  Ops[1] = Builder.getInt32(Imm);
+} else {
+  Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
+  llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
+
+  llvm::Value

[clang] [AArch64][SME] Remove immediate argument restriction for svldr and svstr (PR #68565)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

The svldr_vnum_za and svstr_vnum_za builtins/intrinsics currently require that 
the vnum argument be an immediate, since the instructions take an immediate 
vector number. However, we emit 0 as the immediate for the instruction no 
matter what, and instead modify the base register.

This patch removes that restriction on the argument, so that the argument can 
be a non-immediate. If an appropriate immediate was passed to the builtin then 
CGBuiltin passes that directly to the LLVM intrinsic, otherwise it modifies the 
base register as is existing behaviour.

---
Full diff: https://github.com/llvm/llvm-project/pull/68565.diff


9 Files Affected:

- (modified) clang/include/clang/Basic/arm_sme.td (+2-3) 
- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+29-13) 
- (modified) clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c (+19-7) 
- (modified) clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_str.c (+8-11) 
- (modified) clang/test/Sema/aarch64-sme-intrinsics/acle_sme_imm.cpp (-8) 
- (modified) llvm/include/llvm/IR/IntrinsicsAArch64.td (+2-2) 
- (modified) llvm/lib/Target/AArch64/SMEInstrFormats.td (+5-5) 
- (added) llvm/test/Analysis/CostModel/ARM/unaligned_double_load.ll (+59) 
- (modified) llvm/test/CodeGen/AArch64/sme-intrinsics-loads.ll (+26-7) 


``diff
diff --git a/clang/include/clang/Basic/arm_sme.td 
b/clang/include/clang/Basic/arm_sme.td
index d014900d719c338..49ef6b6b3fc4359 100644
--- a/clang/include/clang/Basic/arm_sme.td
+++ b/clang/include/clang/Basic/arm_sme.td
@@ -44,10 +44,9 @@ defm SVLD1_ZA32 : ZALoad<"za32", "i", "aarch64_sme_ld1w", 
[ImmCheck<0, ImmCheck0
 defm SVLD1_ZA64 : ZALoad<"za64", "l", "aarch64_sme_ld1d", [ImmCheck<0, 
ImmCheck0_7>]>;
 defm SVLD1_ZA128 : ZALoad<"za128", "q", "aarch64_sme_ld1q", [ImmCheck<0, 
ImmCheck0_15>]>;
 
-def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQi", "",
+def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQn", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
-  MemEltTyDefault, "aarch64_sme_ldr",
-  [ImmCheck<2, ImmCheck0_15>]>;
+  MemEltTyDefault, "aarch64_sme_ldr">;
 
 def SVLDR_ZA : MInst<"svldr_za", "vmQ", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d14cf0dccb09982..ca4bf498cab9535 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -9606,7 +9606,7 @@ Value *CodeGenFunction::EmitSVEMaskedStore(const CallExpr 
*E,
 }
 
 Value *CodeGenFunction::EmitTileslice(Value *Offset, Value *Base) {
-  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int32Ty, false);
+  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int64Ty, false);
   return Builder.CreateAdd(Base, CastOffset, "tileslice");
 }
 
@@ -9665,18 +9665,34 @@ Value *CodeGenFunction::EmitSMEZero(const SVETypeFlags 
&TypeFlags,
 Value *CodeGenFunction::EmitSMELdrStr(const SVETypeFlags &TypeFlags,
   SmallVectorImpl &Ops,
   unsigned IntID) {
-  if (Ops.size() == 3) {
-Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
-llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
-llvm::Value *MulVL = Builder.CreateMul(
-CntsbCall,
-Builder.getInt64(cast(Ops[2])->getZExtValue()),
-"mulvl");
-
-Ops[1] = Builder.CreateGEP(Int8Ty, Ops[1], MulVL);
-Ops[0] = EmitTileslice(Ops[0], Ops[2]);
-Ops.erase(&Ops[2]);
-  }
+  if (Ops.size() == 2) {
+// Intrinsics without a vecnum also use this function, so just provide 0
+Ops.push_back(Ops[1]);
+Ops[1] = Builder.getInt32(0);
+  } else {
+int Imm = -1;
+if (ConstantInt* C = dyn_cast(Ops[2]))
+  if (C->getZExtValue() <= 15)
+  Imm = C->getZExtValue();
+
+if (Imm != -1) {
+  Ops[2] = Ops[1];
+  Ops[1] = Builder.getInt32(Imm);
+} else {
+  Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
+  llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
+
+  llvm::Value *VecNum = Ops[2];
+  llvm::Value *MulVL = Builder.CreateMul(
+  CntsbCall,
+  VecNum,
+  "mulvl");
+
+  Ops[2] = Builder.CreateGEP(Int8Ty, Ops[1], MulVL);
+  Ops[1] = Builder.getInt32(0);
+  Ops[0] = Builder.CreateIntCast(EmitTileslice(Ops[0], VecNum), Int32Ty, 
false);
+}
+   }
   Function *F = CGM.getIntrinsic(IntID, {});
   return Builder.CreateCall(F, Ops);
 }
diff --git a/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c 
b/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c
index acddc2ef50a3ddf..df7ff4ca995b544 100644
--- a/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c
+++ b/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c
@@ -8,7 +8,7 @@
 // CHECK-C-LABEL: @test_svldr_vnum

[clang] [AArch64][SME] Remove immediate argument restriction for svldr and svstr (PR #68565)

2023-10-09 Thread Sam Tebbs via cfe-commits

https://github.com/SamTebbs33 updated 
https://github.com/llvm/llvm-project/pull/68565

>From f57f952989ee64d419dc51e9ecf9786720ece3ff Mon Sep 17 00:00:00 2001
From: Samuel Tebbs 
Date: Fri, 6 Oct 2023 17:09:36 +0100
Subject: [PATCH 1/2] [AArch64][SME] Remove immediate argument restriction for
 svldr and svstr

The svldr_vnum_za and svstr_vnum_za builtins/intrinsics currently
require that the vnum argument be an immediate, since the instructions
take an immediate vector number. However, we emit 0 as the immediate
for the instruction no matter what, and instead modify the base register.

This patch removes that restriction on the argument, so that the
argument can be a non-immediate. If an appropriate immediate was
passed to the builtin then CGBuiltin passes that directly to the LLVM
intrinsic, otherwise it modifies the base register as is existing
behaviour.
---
 clang/include/clang/Basic/arm_sme.td  |  5 +-
 clang/lib/CodeGen/CGBuiltin.cpp   | 42 +
 .../aarch64-sme-intrinsics/acle_sme_ldr.c | 26 +---
 .../aarch64-sme-intrinsics/acle_sme_str.c | 19 +++---
 .../aarch64-sme-intrinsics/acle_sme_imm.cpp   |  8 ---
 llvm/include/llvm/IR/IntrinsicsAArch64.td |  4 +-
 llvm/lib/Target/AArch64/SMEInstrFormats.td| 10 ++--
 .../CostModel/ARM/unaligned_double_load.ll| 59 +++
 .../CodeGen/AArch64/sme-intrinsics-loads.ll   | 33 ---
 9 files changed, 150 insertions(+), 56 deletions(-)
 create mode 100644 llvm/test/Analysis/CostModel/ARM/unaligned_double_load.ll

diff --git a/clang/include/clang/Basic/arm_sme.td 
b/clang/include/clang/Basic/arm_sme.td
index d014900d719c338..49ef6b6b3fc4359 100644
--- a/clang/include/clang/Basic/arm_sme.td
+++ b/clang/include/clang/Basic/arm_sme.td
@@ -44,10 +44,9 @@ defm SVLD1_ZA32 : ZALoad<"za32", "i", "aarch64_sme_ld1w", 
[ImmCheck<0, ImmCheck0
 defm SVLD1_ZA64 : ZALoad<"za64", "l", "aarch64_sme_ld1d", [ImmCheck<0, 
ImmCheck0_7>]>;
 defm SVLD1_ZA128 : ZALoad<"za128", "q", "aarch64_sme_ld1q", [ImmCheck<0, 
ImmCheck0_15>]>;
 
-def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQi", "",
+def SVLDR_VNUM_ZA : MInst<"svldr_vnum_za", "vmQn", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
-  MemEltTyDefault, "aarch64_sme_ldr",
-  [ImmCheck<2, ImmCheck0_15>]>;
+  MemEltTyDefault, "aarch64_sme_ldr">;
 
 def SVLDR_ZA : MInst<"svldr_za", "vmQ", "",
   [IsOverloadNone, IsStreamingCompatible, IsSharedZA],
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d14cf0dccb09982..ca4bf498cab9535 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -9606,7 +9606,7 @@ Value *CodeGenFunction::EmitSVEMaskedStore(const CallExpr 
*E,
 }
 
 Value *CodeGenFunction::EmitTileslice(Value *Offset, Value *Base) {
-  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int32Ty, false);
+  llvm::Value *CastOffset = Builder.CreateIntCast(Offset, Int64Ty, false);
   return Builder.CreateAdd(Base, CastOffset, "tileslice");
 }
 
@@ -9665,18 +9665,34 @@ Value *CodeGenFunction::EmitSMEZero(const SVETypeFlags 
&TypeFlags,
 Value *CodeGenFunction::EmitSMELdrStr(const SVETypeFlags &TypeFlags,
   SmallVectorImpl &Ops,
   unsigned IntID) {
-  if (Ops.size() == 3) {
-Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
-llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
-llvm::Value *MulVL = Builder.CreateMul(
-CntsbCall,
-Builder.getInt64(cast(Ops[2])->getZExtValue()),
-"mulvl");
-
-Ops[1] = Builder.CreateGEP(Int8Ty, Ops[1], MulVL);
-Ops[0] = EmitTileslice(Ops[0], Ops[2]);
-Ops.erase(&Ops[2]);
-  }
+  if (Ops.size() == 2) {
+// Intrinsics without a vecnum also use this function, so just provide 0
+Ops.push_back(Ops[1]);
+Ops[1] = Builder.getInt32(0);
+  } else {
+int Imm = -1;
+if (ConstantInt* C = dyn_cast(Ops[2]))
+  if (C->getZExtValue() <= 15)
+  Imm = C->getZExtValue();
+
+if (Imm != -1) {
+  Ops[2] = Ops[1];
+  Ops[1] = Builder.getInt32(Imm);
+} else {
+  Function *Cntsb = CGM.getIntrinsic(Intrinsic::aarch64_sme_cntsb);
+  llvm::Value *CntsbCall = Builder.CreateCall(Cntsb, {}, "svlb");
+
+  llvm::Value *VecNum = Ops[2];
+  llvm::Value *MulVL = Builder.CreateMul(
+  CntsbCall,
+  VecNum,
+  "mulvl");
+
+  Ops[2] = Builder.CreateGEP(Int8Ty, Ops[1], MulVL);
+  Ops[1] = Builder.getInt32(0);
+  Ops[0] = Builder.CreateIntCast(EmitTileslice(Ops[0], VecNum), Int32Ty, 
false);
+}
+   }
   Function *F = CGM.getIntrinsic(IntID, {});
   return Builder.CreateCall(F, Ops);
 }
diff --git a/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c 
b/clang/test/CodeGen/aarch64-sme-intrinsics/acle_sme_ldr.c
index acddc2ef50a3d

[clang] [clang][Sema] Emit more specific diagnostic for auto in lambda before C++14 (#46059) (PR #68540)

2023-10-09 Thread via cfe-commits

cor3ntin wrote:

Thanks for this patch!

Our policy is to not mix functional changes and formatting changes.
Could you change the PR to have only the changes you made, wit no formatting 
change?
You can format your patch with `git clang-format` which only formats the lines 
that you changed.


https://github.com/llvm/llvm-project/pull/68540
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [C++20] [Modules] Don't emit function bodies which is noinline and av… (PR #68501)

2023-10-09 Thread Iain Sandoe via cfe-commits

https://github.com/iains approved this pull request.

I think it would be better to have a coherent plan to deal with the underlying 
issue; starting with separating the AST used for code-gen from that used for 
interfaces.  Having said that, this LGTM (it will be interesting to see what 
performance gains are seen in practice).


https://github.com/llvm/llvm-project/pull/68501
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Frontend] Fix a crash when -Wdocumentation is used (PR #68525)

2023-10-09 Thread via cfe-commits

cor3ntin wrote:

Is there a way we could come up with a test for this?

https://github.com/llvm/llvm-project/pull/68525
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] llvm-canon (PR #68176)

2023-10-09 Thread Nikita Popov via cfe-commits

nikic wrote:

Using the term for MIR is okay, because it does not have a pre-existing notion 
of canonical form. Doing the same for IR is not okay, because it already has a 
canonical form and this pass does not produce it. Calling this `ir-normalizer` 
instead of `ir-canonicalizer` should convey about the same intent without 
overloading an existing, widely used term.

https://github.com/llvm/llvm-project/pull/68176
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] llvm-canon (PR #68176)

2023-10-09 Thread Nikita Popov via cfe-commits

nikic wrote:

Using the term for MIR is okay, because it does not have a pre-existing notion 
of canonical form. Doing the same for IR is not okay, because it already has a 
canonical form and this pass does not produce it. Calling this `ir-normalizer` 
instead of `ir-canonicalizer` should convey about the same intent without 
overloading an existing, widely used term.

https://github.com/llvm/llvm-project/pull/68176
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] e0809bd - Fix Wparentheses warning. NFC.

2023-10-09 Thread Simon Pilgrim via cfe-commits

Author: Simon Pilgrim
Date: 2023-10-09T10:26:00+01:00
New Revision: e0809bd062220eb2eeb8ca23d3731839e18f3eee

URL: 
https://github.com/llvm/llvm-project/commit/e0809bd062220eb2eeb8ca23d3731839e18f3eee
DIFF: 
https://github.com/llvm/llvm-project/commit/e0809bd062220eb2eeb8ca23d3731839e18f3eee.diff

LOG: Fix Wparentheses warning. NFC.

Added: 


Modified: 
clang/lib/Sema/SemaDecl.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c6cb04b64f545e0..23b743d67a16b07 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -10457,10 +10457,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator 
&D, DeclContext *DC,
  TemplateSpecializationType::
  anyInstantiationDependentTemplateArguments(
  TemplateArgs.arguments()));
-assert(!isDependentSpecialization ||
-   (HasExplicitTemplateArgs == isDependentSpecialization) &&
-   "dependent friend function specialization without template "
-   "args");
+assert((!isDependentSpecialization ||
+(HasExplicitTemplateArgs == isDependentSpecialization)) &&
+   "dependent friend function specialization without template "
+   "args");
   } else {
 // For class-scope explicit specializations of function templates,
 // if the lexical context is dependent, then the specialization



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][NFC] Remove outdated FIXME comment (PR #68211)

2023-10-09 Thread Endre Fülöp via cfe-commits

https://github.com/gamesh411 approved this pull request.

I can confirm the claims about HeapSpaceRegion being used:
![image](https://github.com/llvm/llvm-project/assets/3802256/682c69f1-7129-4d0f-873f-44aee0fa5f2f)



https://github.com/llvm/llvm-project/pull/68211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][NFC] Remove outdated FIXME comment (PR #68211)

2023-10-09 Thread Endre Fülöp via cfe-commits

https://github.com/gamesh411 edited 
https://github.com/llvm/llvm-project/pull/68211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -45,6 +45,10 @@ class GlobalCompilationDatabase {
 return std::nullopt;
   }
 
+  virtual std::vector getAllFilesInProjectOf(PathRef File) const {

ChuanqiXu9 wrote:

Done by adding ProjectModules class in ProjectModules.cpp file.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);

ChuanqiXu9 wrote:

Yeah, I added `ModuleDependencyInfo` struct.

```
struct ModuleDependencyInfo {
// The name of the module if the file is a module unit.
std::optional ModuleName;
// A list of names for the modules that the file directly depends.
std::vector RequiredModules;
  };
```

The complete P1689 information may be redundant for us.

I didn't add the suggested interfaces. Since I found the following two 
interfaces are sufficient and more straight forward in practice (from my mind. 
I feel like we don't need to look at the definition of `ModuleNode` now when we 
use the scanner):

```
PathRef getSourceForModuleName(StringRef ModuleName);
std::vector getRequiredModules(PathRef File);
```



https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)

ChuanqiXu9 wrote:

Done

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.

ChuanqiXu9 wrote:

Done

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);
+
+  /// Scanning every source file in the current project to get the
+  ///  to  map.
+  /// It looks unefficiency to scan the whole project especially for
+  /// every version of every file!
+  /// TODO: We should find a efficient method to get the 
+  /// to  map. We can make it either by providing
+  /// a global module dependency scanner to monitor every file. Or we
+  /// can simply require the build systems (or even if the end users)
+  /// to provide the map.
+  void globalScan(PathRef File);

ChuanqiXu9 wrote:

Now the scanner are only used by `ScanningAllProjectModules` class. In the 
scanner itself, I add the assertion in `getSourceForModuleName `. And in the 
`ScanningAllProjectModules` class, I make it to call globalScan if that hasn't 
happened already.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);
+
+  /// Scanning every source file in the current project to get the
+  ///  to  map.
+  /// It looks unefficiency to scan the whole project especially for
+  /// every version of every file!
+  /// TODO: We should find a efficient method to get the 
+  /// to  map. We can make it either by providing
+  /// a global module dependency scanner to monitor every file. Or we
+  /// can simply require the build systems (or even if the end users)
+  /// to provide the map.
+  void globalScan(PathRef File);
+
+  PathRef getSourceForModuleName(StringRef ModuleName) const;
+
+  /// Return the direct required modules. Indirect required modules are not
+  /// included.
+  std::vector getRequiredModules(PathRef File) const;

ChuanqiXu9 wrote:

Done. I've removed the `getModuleName` function now.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);
+
+  /// Scanning every source file in the current project to get the
+  ///  to  map.
+  /// It looks unefficiency to scan the whole project especially for
+  /// every version of every file!
+  /// TODO: We should find a efficient method to get the 
+  /// to  map. We can make it either by providing
+  /// a global module dependency scanner to monitor every file. Or we
+  /// can simply require the build systems (or even if the end users)
+  /// to provide the map.
+  void globalScan(PathRef File);
+
+  PathRef getSourceForModuleName(StringRef ModuleName) const;
+
+  /// Return the direct required modules. Indirect required modules are not
+  /// included.
+  std::vector getRequiredModules(PathRef File) const;
+  StringRef getModuleName(PathRef File) const;
+
+  const ThreadsafeFS *getThreadsafeFS() const { return TFS; }

ChuanqiXu9 wrote:

Done.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -442,6 +448,9 @@ ParsedAST::build(llvm::StringRef Filename, const 
ParseInputs &Inputs,
   std::move(CI), PreamblePCH,
   llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS,
   *DiagConsumer);
+
+  // Clangd Modules TODO: refactor the command line options of `Clang` here.

ChuanqiXu9 wrote:

Done.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits

https://github.com/ChuanqiXu9 edited 
https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed
+/// module files. All the built module files won't be shared with other source 
files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or 
`ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built 
module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;
+  ~ModuleFilesInfo();
+
+  ModuleFilesInfo(const ModuleFilesInfo &) = delete;
+  ModuleFilesInfo operator=(const ModuleFilesInfo &) = delete;
+
+  ModuleFilesInfo(ModuleFilesInfo &&Other)
+  : Successed(std::exchange(Other.Successed, false)),
+UniqueModuleFilesPathPrefix(
+std::move(Other.UniqueModuleFilesPathPrefix)),
+DependentModuleNames(std::move(Other.DependentModuleNames)) {
+Other.UniqueModuleFilesPathPrefix.clear();
+  }
+  ModuleFilesInfo &operator=(ModuleFilesInfo &&Other) {
+if (this == &Other)
+  return *this;
+
+this->~ModuleFilesInfo();
+new (this) ModuleFilesInfo(std::move(Other));
+
+return *this;
+  }
+
+  /// Build all the required module files for \param File.
+  /// Note that only the module files recorded by \param CDB can be built.
+  static ModuleFilesInfo
+  buildModuleFilesInfoFor(PathRef File, const ThreadsafeFS *TFS,
+  const GlobalCompilationDatabase &CDB);
+
+  /// Return true if the modile file specified by ModuleName is built.
+  /// Note that this interface will only check the existence of the module
+  /// file instead of checking the validness of the module file. 
+  bool IsModuleUnitBuilt(StringRef ModuleName) const;
+
+  /// Change commands to load the module files recorded in this ModuleFilesInfo

ChuanqiXu9 wrote:

Done.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed
+/// module files. All the built module files won't be shared with other source 
files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or 
`ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built 
module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;
+  ~ModuleFilesInfo();
+
+  ModuleFilesInfo(const ModuleFilesInfo &) = delete;
+  ModuleFilesInfo operator=(const ModuleFilesInfo &) = delete;
+
+  ModuleFilesInfo(ModuleFilesInfo &&Other)
+  : Successed(std::exchange(Other.Successed, false)),
+UniqueModuleFilesPathPrefix(
+std::move(Other.UniqueModuleFilesPathPrefix)),
+DependentModuleNames(std::move(Other.DependentModuleNames)) {
+Other.UniqueModuleFilesPathPrefix.clear();
+  }
+  ModuleFilesInfo &operator=(ModuleFilesInfo &&Other) {
+if (this == &Other)
+  return *this;
+
+this->~ModuleFilesInfo();
+new (this) ModuleFilesInfo(std::move(Other));
+
+return *this;
+  }
+
+  /// Build all the required module files for \param File.
+  /// Note that only the module files recorded by \param CDB can be built.
+  static ModuleFilesInfo
+  buildModuleFilesInfoFor(PathRef File, const ThreadsafeFS *TFS,
+  const GlobalCompilationDatabase &CDB);
+
+  /// Return true if the modile file specified by ModuleName is built.
+  /// Note that this interface will only check the existence of the module
+  /// file instead of checking the validness of the module file. 
+  bool IsModuleUnitBuilt(StringRef ModuleName) const;

ChuanqiXu9 wrote:

Done.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed
+/// module files. All the built module files won't be shared with other source 
files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or 
`ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built 
module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;
+  ~ModuleFilesInfo();
+
+  ModuleFilesInfo(const ModuleFilesInfo &) = delete;
+  ModuleFilesInfo operator=(const ModuleFilesInfo &) = delete;
+
+  ModuleFilesInfo(ModuleFilesInfo &&Other)
+  : Successed(std::exchange(Other.Successed, false)),
+UniqueModuleFilesPathPrefix(
+std::move(Other.UniqueModuleFilesPathPrefix)),
+DependentModuleNames(std::move(Other.DependentModuleNames)) {
+Other.UniqueModuleFilesPathPrefix.clear();
+  }
+  ModuleFilesInfo &operator=(ModuleFilesInfo &&Other) {
+if (this == &Other)
+  return *this;
+
+this->~ModuleFilesInfo();
+new (this) ModuleFilesInfo(std::move(Other));
+
+return *this;
+  }
+
+  /// Build all the required module files for \param File.
+  /// Note that only the module files recorded by \param CDB can be built.
+  static ModuleFilesInfo
+  buildModuleFilesInfoFor(PathRef File, const ThreadsafeFS *TFS,
+  const GlobalCompilationDatabase &CDB);
+
+  /// Return true if the modile file specified by ModuleName is built.
+  /// Note that this interface will only check the existence of the module
+  /// file instead of checking the validness of the module file. 
+  bool IsModuleUnitBuilt(StringRef ModuleName) const;
+
+  /// Change commands to load the module files recorded in this ModuleFilesInfo
+  /// first.
+  void ReplaceHeaderSearchOptions(HeaderSearchOptions &Options) const;

ChuanqiXu9 wrote:

Only the first function `ReplaceHeaderSearchOptions` are used outside in 
`ParsedAST::build` and the following two functions are only used internally and 
in the unittests.

> can this be a single thing on a CompilerInstance?

I feel it is not good to do this on CompilerInstance. Since, (in my mind), what 
we do actually is about invocation instead of touching the compiler instance 
actually.

> Having ParsedAST know exactly what needs to be tweaked is a bit leaky.

I feel it is not bad since only the `ParsedAST::build` (an static member 
function) uses this.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);
+
+  /// Scanning every source file in the current project to get the
+  ///  to  map.
+  /// It looks unefficiency to scan the whole project especially for
+  /// every version of every file!
+  /// TODO: We should find a efficient method to get the 
+  /// to  map. We can make it either by providing
+  /// a global module dependency scanner to monitor every file. Or we
+  /// can simply require the build systems (or even if the end users)
+  /// to provide the map.
+  void globalScan(PathRef File);
+
+  PathRef getSourceForModuleName(StringRef ModuleName) const;
+
+  /// Return the direct required modules. Indirect required modules are not
+  /// included.
+  std::vector getRequiredModules(PathRef File) const;
+  StringRef getModuleName(PathRef File) const;
+
+  const ThreadsafeFS *getThreadsafeFS() const { return TFS; }
+
+  const GlobalCompilationDatabase &getCompilationDatabase() const { return 
CDB; }
+
+private:
+  const GlobalCompilationDatabase &CDB;
+  const ThreadsafeFS *TFS;
+
+  clang::tooling::dependencies::DependencyScanningService Service;
+
+  // Map source file to P1689 Result.
+  llvm::StringMap ScanningCache;
+  // Map module name to source file path.
+  llvm::StringMap ModuleNameToSourceMapper;

ChuanqiXu9 wrote:

Do you mean renaming it to `ModuleNameToSource`? (That's what I did)

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,78 @@
+//===-- ModuleDependencyScanner.h *- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEDEPENDENCYSCANNER_H
+
+#include "GlobalCompilationDatabase.h"
+#include "support/Path.h"
+#include "support/ThreadsafeFS.h"
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// A scanner to produce P1689 format for C++20 Modules.
+///
+/// The scanner can scan a single file with `scan(PathRef)` member function
+/// or scan the whole project with `globalScan(PathRef)` member function. See
+/// the comments of `globalScan` to see the details.
+class ModuleDependencyScanner {
+public:
+  ModuleDependencyScanner(const GlobalCompilationDatabase &CDB,
+  const ThreadsafeFS *TFS)
+  : CDB(CDB), TFS(TFS),
+Service(tooling::dependencies::ScanningMode::CanonicalPreprocessing,
+tooling::dependencies::ScanningOutputFormat::P1689) {}
+
+  /// Scanning the single file specified by \param FilePath.
+  std::optional scan(PathRef 
FilePath);
+
+  /// Scanning every source file in the current project to get the
+  ///  to  map.
+  /// It looks unefficiency to scan the whole project especially for
+  /// every version of every file!
+  /// TODO: We should find a efficient method to get the 
+  /// to  map. We can make it either by providing
+  /// a global module dependency scanner to monitor every file. Or we
+  /// can simply require the build systems (or even if the end users)
+  /// to provide the map.
+  void globalScan(PathRef File);
+
+  PathRef getSourceForModuleName(StringRef ModuleName) const;
+
+  /// Return the direct required modules. Indirect required modules are not
+  /// included.
+  std::vector getRequiredModules(PathRef File) const;
+  StringRef getModuleName(PathRef File) const;
+
+  const ThreadsafeFS *getThreadsafeFS() const { return TFS; }
+
+  const GlobalCompilationDatabase &getCompilationDatabase() const { return 
CDB; }
+
+private:
+  const GlobalCompilationDatabase &CDB;
+  const ThreadsafeFS *TFS;
+
+  clang::tooling::dependencies::DependencyScanningService Service;
+
+  // Map source file to P1689 Result.

ChuanqiXu9 wrote:

Done by removing the cache. I've added a TODO for this.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.

ChuanqiXu9 wrote:

Done by rewriting the comments.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//

ChuanqiXu9 wrote:

Done by renaming. I don't quite understand the sentence "the modules are the 
inputs for something in particular.".

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed

ChuanqiXu9 wrote:

Done. Personally I prefer a static member function than a free function for 
`creating` methods.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed
+/// module files. All the built module files won't be shared with other source 
files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or 
`ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built 
module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;

ChuanqiXu9 wrote:

Done. Now the default constructor is private. But it is still movable since we 
need to assign it to Preamble.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,118 @@
+//===- ModuleFilesInfo.h -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Experimental support for C++20 Modules.
+//
+// Currently we simplify the implementations by preventing reusing module files
+// across different versions and different source files. But this is clearly a
+// waste of time and space in the end of the day.
+//
+// FIXME: Supporting reusing module files across different versions and 
different
+// source files.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_MODULEFILESINFO_H
+
+#include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
+#include "ModuleDependencyScanner.h"
+#include "support/Path.h"
+
+#include "clang/Lex/HeaderSearchOptions.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringMap.h"
+
+namespace clang {
+namespace clangd {
+
+/// Store module files information for a single file.
+///
+/// A ModuleFilesInfo should only be initialized by
+/// `ModuleFilesInfo::buildModuleFilesInfoFor(PathRef, const ThreadsafeFS *, 
const GlobalCompilationDatabase &)`
+/// static member method. This method will block the current thread and build 
all the needed
+/// module files. All the built module files won't be shared with other source 
files.
+///
+/// Users can detect whether the ModuleFilesInfo is still up to date by calling
+/// the `CanReuse()` member function.
+///
+/// The users should call `ReplaceHeaderSearchOptions(...)` or 
`ReplaceCompileCommands(CompileCommand&)`
+/// member function to update the compilation commands to select the built 
module files first.
+struct ModuleFilesInfo {
+  ModuleFilesInfo() = default;
+  ~ModuleFilesInfo();
+
+  ModuleFilesInfo(const ModuleFilesInfo &) = delete;
+  ModuleFilesInfo operator=(const ModuleFilesInfo &) = delete;
+
+  ModuleFilesInfo(ModuleFilesInfo &&Other)
+  : Successed(std::exchange(Other.Successed, false)),
+UniqueModuleFilesPathPrefix(
+std::move(Other.UniqueModuleFilesPathPrefix)),
+DependentModuleNames(std::move(Other.DependentModuleNames)) {
+Other.UniqueModuleFilesPathPrefix.clear();
+  }
+  ModuleFilesInfo &operator=(ModuleFilesInfo &&Other) {
+if (this == &Other)
+  return *this;
+
+this->~ModuleFilesInfo();
+new (this) ModuleFilesInfo(std::move(Other));
+
+return *this;
+  }
+
+  /// Build all the required module files for \param File.
+  /// Note that only the module files recorded by \param CDB can be built.
+  static ModuleFilesInfo
+  buildModuleFilesInfoFor(PathRef File, const ThreadsafeFS *TFS,
+  const GlobalCompilationDatabase &CDB);
+
+  /// Return true if the modile file specified by ModuleName is built.
+  /// Note that this interface will only check the existence of the module
+  /// file instead of checking the validness of the module file. 
+  bool IsModuleUnitBuilt(StringRef ModuleName) const;

ChuanqiXu9 wrote:

This is used in unittests. I've added a comment.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,282 @@
+//===- ModuleFilesInfo.cpp ---*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ModuleFilesInfo.h"
+#include "support/Logger.h"
+
+#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Serialization/ASTReader.h"
+
+namespace clang {
+namespace clangd {
+
+namespace {
+llvm::SmallString<128> getAbsolutePath(const tooling::CompileCommand &Cmd) {
+  llvm::SmallString<128> AbsolutePath;
+  if (llvm::sys::path::is_absolute(Cmd.Filename)) {
+AbsolutePath = Cmd.Filename;
+  } else {
+AbsolutePath = Cmd.Directory;
+llvm::sys::path::append(AbsolutePath, Cmd.Filename);
+llvm::sys::path::remove_dots(AbsolutePath, true);
+  }
+  return AbsolutePath;
+}
+} // namespace
+
+ModuleFilesInfo::ModuleFilesInfo(PathRef MainFile,
+ const GlobalCompilationDatabase &CDB) {
+  std::optional PI = CDB.getProjectInfo(MainFile);
+  if (!PI)
+return;
+
+  llvm::SmallString<128> Result(PI->SourceRoot);
+  llvm::sys::path::append(Result, ".cache");
+  llvm::sys::path::append(Result, "clangd");
+  llvm::sys::path::append(Result, "module_files");
+  llvm::sys::fs::create_directories(Result, /*IgnoreExisting=*/true);
+
+  llvm::sys::path::append(Result, llvm::sys::path::filename(MainFile));
+  llvm::sys::fs::createUniqueDirectory(Result, UniqueModuleFilesPathPrefix);
+
+  log("Initialized module files to {0}", UniqueModuleFilesPathPrefix.str());
+}
+
+ModuleFilesInfo::~ModuleFilesInfo() {
+  DependentModuleNames.clear();
+  Successed = false;
+
+  if (UniqueModuleFilesPathPrefix.empty())
+return;
+
+  llvm::sys::fs::remove_directories(UniqueModuleFilesPathPrefix);
+  UniqueModuleFilesPathPrefix.clear();
+}
+
+llvm::SmallString<256>
+ModuleFilesInfo::getModuleFilePath(StringRef ModuleName) const {
+  llvm::SmallString<256> ModuleFilePath;
+
+  ModuleFilePath = UniqueModuleFilesPathPrefix;
+  auto [PrimaryModuleName, PartitionName] = ModuleName.split(':');
+  llvm::sys::path::append(ModuleFilePath, PrimaryModuleName);
+  if (!PartitionName.empty()) {
+ModuleFilePath.append("-");
+ModuleFilePath.append(PartitionName);
+  }
+  ModuleFilePath.append(".pcm");
+
+  return ModuleFilePath;
+}
+
+bool ModuleFilesInfo::IsModuleUnitBuilt(StringRef ModuleName) const {
+  if (!DependentModuleNames.count(ModuleName))
+return false;
+
+  auto BMIPath = getModuleFilePath(ModuleName);
+  if (llvm::sys::fs::exists(BMIPath))
+return true;
+
+  Successed = false;
+
+  DependentModuleNames.erase(ModuleName);
+  return false;
+}
+
+void ModuleFilesInfo::ReplaceHeaderSearchOptions(
+HeaderSearchOptions &Options) const {
+  if (!IsInited())
+return;
+
+  Options.PrebuiltModulePaths.insert(Options.PrebuiltModulePaths.begin(),
+ UniqueModuleFilesPathPrefix.str().str());
+
+  for (auto Iter = Options.PrebuiltModuleFiles.begin();
+   Iter != Options.PrebuiltModuleFiles.end();) {
+if (IsModuleUnitBuilt(Iter->first)) {
+  Iter = Options.PrebuiltModuleFiles.erase(Iter);
+  continue;
+}
+
+Iter++;
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(
+tooling::CompileCommand &Cmd) const {
+  if (!IsInited())
+return;
+
+  std::vector CommandLine(std::move(Cmd.CommandLine));
+
+  Cmd.CommandLine.emplace_back(CommandLine[0]);
+  Cmd.CommandLine.emplace_back(
+  llvm::Twine("-fprebuilt-module-path=" + UniqueModuleFilesPathPrefix)
+  .str());
+
+  for (std::size_t I = 1; I < CommandLine.size(); I++) {
+const std::string &Arg = CommandLine[I];
+const auto &[LHS, RHS] = StringRef(Arg).split("=");
+
+// Remove original `-fmodule-file==` form if it
+// already built.
+if (LHS == "-fmodule-file" && RHS.contains("=")) {
+  const auto &[ModuleName, _] = RHS.split("=");
+  if (IsModuleUnitBuilt(ModuleName))
+continue;
+}
+
+Cmd.CommandLine.emplace_back(Arg);
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(tooling::CompileCommand &Cmd,
+ StringRef OutputModuleName) const 
{
+  if (!IsInited())
+return;
+
+  ReplaceCompileCommands(Cmd);
+
+  Cmd.Output = getModuleFilePath(OutputModuleName).str().str();
+}
+
+bool ModuleFilesInfo::buildModuleFile(PathRef ModuleUnitFileName,
+  ModuleDependencyScanner &Scanner) {
+  if (ModuleUnitFileName.empty())
+return false;
+
+  for (auto &ModuleName : Scanner.getRequiredModules(ModuleUnitFileName)) {
+// Return early if there are errors building the module file.
+if (!IsModuleUnitBuilt(ModuleName) &&
+!buildModuleFile(Scanner.getSourceForModuleN

[clang-tools-extra] [OpenMPIRBuilder] Remove wrapper function in `createTask`, `createTeams` (PR #67723)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan approved this pull request.

Thanks for the detailed summary. LG.

https://github.com/llvm/llvm-project/pull/67723
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [OpenMPIRBuilder] Remove wrapper function in `createTask`, `createTeams` (PR #67723)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan approved this pull request.

Thanks for the detailed summary. LG.

https://github.com/llvm/llvm-project/pull/67723
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [OpenMPIRBuilder] Remove wrapper function in `createTask`, `createTeams` (PR #67723)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan approved this pull request.

Thanks for the detailed summary. LG.

https://github.com/llvm/llvm-project/pull/67723
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Frontend] Fix a crash when -Wdocumentation is used (PR #68525)

2023-10-09 Thread Byoungchan Lee via cfe-commits

bc-lee wrote:

> Is there a way we could come up with a test for this?

Unfortunately, I don't think so. I cannot reduce the 600k lines of preprocessed 
code to a small test case that will crash the clang frontend.

https://github.com/llvm/llvm-project/pull/68525
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] [MinGW] Explicitly always pass the -fno-use-init-array (PR #68571)

2023-10-09 Thread Martin Storsjö via cfe-commits

https://github.com/mstorsjo created 
https://github.com/llvm/llvm-project/pull/68571

On MinGW targets, the .ctors section is always used for constructors.

Make sure that all layers of code generation is aware of this, wherever it 
matters, by passing the -fno-use-init-array option, setting the TargetOptions 
field UseInitArray to false.

This fixes https://github.com/llvm/llvm-project/issues/55938.

From 89521ab0bfd12d7f45cacd3fa1d6c3f27ff54ab6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= 
Date: Mon, 9 Oct 2023 13:13:28 +0300
Subject: [PATCH] [clang] [MinGW] Explicitly always pass the
 -fno-use-init-array

On MinGW targets, the .ctors section is always used for constructors.

Make sure that all layers of code generation is aware of this,
wherever it matters, by passing the -fno-use-init-array option,
setting the TargetOptions field UseInitArray to false.

This fixes https://github.com/llvm/llvm-project/issues/55938.
---
 clang/lib/Driver/ToolChains/MinGW.cpp | 2 ++
 clang/test/Driver/mingw.cpp   | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp 
b/clang/lib/Driver/ToolChains/MinGW.cpp
index 5872e13bda358f8..d3d829a8ddbdb95 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -709,6 +709,8 @@ void toolchains::MinGW::addClangTargetOptions(
 }
   }
 
+  CC1Args.push_back("-fno-use-init-array");
+
   for (auto Opt : {options::OPT_mthreads, options::OPT_mwindows,
options::OPT_mconsole, options::OPT_mdll}) {
 if (Arg *A = DriverArgs.getLastArgNoClaim(Opt))
diff --git a/clang/test/Driver/mingw.cpp b/clang/test/Driver/mingw.cpp
index 0f276820d0fff22..bb22a0652b486e1 100644
--- a/clang/test/Driver/mingw.cpp
+++ b/clang/test/Driver/mingw.cpp
@@ -77,3 +77,6 @@
 // CHECK_NO_SUBSYS-NOT: "--subsystem"
 // CHECK_SUBSYS_CONSOLE: "--subsystem" "console"
 // CHECK_SUBSYS_WINDOWS: "--subsystem" "windows"
+
+// RUN: %clang -target i686-windows-gnu -### %s 2>&1 | FileCheck 
-check-prefix=CHECK_NO_INIT_ARRAY %s
+// CHECK_NO_INIT_ARRAY: "-fno-use-init-array"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,282 @@
+//===- ModuleFilesInfo.cpp ---*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ModuleFilesInfo.h"
+#include "support/Logger.h"
+
+#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Serialization/ASTReader.h"
+
+namespace clang {
+namespace clangd {
+
+namespace {
+llvm::SmallString<128> getAbsolutePath(const tooling::CompileCommand &Cmd) {
+  llvm::SmallString<128> AbsolutePath;
+  if (llvm::sys::path::is_absolute(Cmd.Filename)) {
+AbsolutePath = Cmd.Filename;
+  } else {
+AbsolutePath = Cmd.Directory;
+llvm::sys::path::append(AbsolutePath, Cmd.Filename);
+llvm::sys::path::remove_dots(AbsolutePath, true);
+  }
+  return AbsolutePath;
+}
+} // namespace
+
+ModuleFilesInfo::ModuleFilesInfo(PathRef MainFile,
+ const GlobalCompilationDatabase &CDB) {
+  std::optional PI = CDB.getProjectInfo(MainFile);
+  if (!PI)
+return;
+
+  llvm::SmallString<128> Result(PI->SourceRoot);
+  llvm::sys::path::append(Result, ".cache");
+  llvm::sys::path::append(Result, "clangd");
+  llvm::sys::path::append(Result, "module_files");
+  llvm::sys::fs::create_directories(Result, /*IgnoreExisting=*/true);
+
+  llvm::sys::path::append(Result, llvm::sys::path::filename(MainFile));
+  llvm::sys::fs::createUniqueDirectory(Result, UniqueModuleFilesPathPrefix);
+
+  log("Initialized module files to {0}", UniqueModuleFilesPathPrefix.str());
+}
+
+ModuleFilesInfo::~ModuleFilesInfo() {
+  DependentModuleNames.clear();
+  Successed = false;
+
+  if (UniqueModuleFilesPathPrefix.empty())
+return;
+
+  llvm::sys::fs::remove_directories(UniqueModuleFilesPathPrefix);
+  UniqueModuleFilesPathPrefix.clear();
+}
+
+llvm::SmallString<256>
+ModuleFilesInfo::getModuleFilePath(StringRef ModuleName) const {
+  llvm::SmallString<256> ModuleFilePath;
+
+  ModuleFilePath = UniqueModuleFilesPathPrefix;
+  auto [PrimaryModuleName, PartitionName] = ModuleName.split(':');
+  llvm::sys::path::append(ModuleFilePath, PrimaryModuleName);
+  if (!PartitionName.empty()) {
+ModuleFilePath.append("-");
+ModuleFilePath.append(PartitionName);
+  }
+  ModuleFilePath.append(".pcm");
+
+  return ModuleFilePath;
+}
+
+bool ModuleFilesInfo::IsModuleUnitBuilt(StringRef ModuleName) const {
+  if (!DependentModuleNames.count(ModuleName))
+return false;
+
+  auto BMIPath = getModuleFilePath(ModuleName);
+  if (llvm::sys::fs::exists(BMIPath))
+return true;
+
+  Successed = false;
+
+  DependentModuleNames.erase(ModuleName);
+  return false;
+}
+
+void ModuleFilesInfo::ReplaceHeaderSearchOptions(
+HeaderSearchOptions &Options) const {
+  if (!IsInited())
+return;
+
+  Options.PrebuiltModulePaths.insert(Options.PrebuiltModulePaths.begin(),
+ UniqueModuleFilesPathPrefix.str().str());
+
+  for (auto Iter = Options.PrebuiltModuleFiles.begin();
+   Iter != Options.PrebuiltModuleFiles.end();) {
+if (IsModuleUnitBuilt(Iter->first)) {
+  Iter = Options.PrebuiltModuleFiles.erase(Iter);
+  continue;
+}
+
+Iter++;
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(
+tooling::CompileCommand &Cmd) const {
+  if (!IsInited())
+return;
+
+  std::vector CommandLine(std::move(Cmd.CommandLine));
+
+  Cmd.CommandLine.emplace_back(CommandLine[0]);
+  Cmd.CommandLine.emplace_back(
+  llvm::Twine("-fprebuilt-module-path=" + UniqueModuleFilesPathPrefix)
+  .str());
+
+  for (std::size_t I = 1; I < CommandLine.size(); I++) {
+const std::string &Arg = CommandLine[I];
+const auto &[LHS, RHS] = StringRef(Arg).split("=");
+
+// Remove original `-fmodule-file==` form if it
+// already built.
+if (LHS == "-fmodule-file" && RHS.contains("=")) {
+  const auto &[ModuleName, _] = RHS.split("=");
+  if (IsModuleUnitBuilt(ModuleName))
+continue;
+}
+
+Cmd.CommandLine.emplace_back(Arg);
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(tooling::CompileCommand &Cmd,
+ StringRef OutputModuleName) const 
{
+  if (!IsInited())
+return;
+
+  ReplaceCompileCommands(Cmd);
+
+  Cmd.Output = getModuleFilePath(OutputModuleName).str().str();
+}
+
+bool ModuleFilesInfo::buildModuleFile(PathRef ModuleUnitFileName,
+  ModuleDependencyScanner &Scanner) {
+  if (ModuleUnitFileName.empty())
+return false;
+
+  for (auto &ModuleName : Scanner.getRequiredModules(ModuleUnitFileName)) {
+// Return early if there are errors building the module file.
+if (!IsModuleUnitBuilt(ModuleName) &&
+!buildModuleFile(Scanner.getSourceForModuleN

[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits


@@ -0,0 +1,282 @@
+//===- ModuleFilesInfo.cpp ---*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ModuleFilesInfo.h"
+#include "support/Logger.h"
+
+#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Serialization/ASTReader.h"
+
+namespace clang {
+namespace clangd {
+
+namespace {
+llvm::SmallString<128> getAbsolutePath(const tooling::CompileCommand &Cmd) {
+  llvm::SmallString<128> AbsolutePath;
+  if (llvm::sys::path::is_absolute(Cmd.Filename)) {
+AbsolutePath = Cmd.Filename;
+  } else {
+AbsolutePath = Cmd.Directory;
+llvm::sys::path::append(AbsolutePath, Cmd.Filename);
+llvm::sys::path::remove_dots(AbsolutePath, true);
+  }
+  return AbsolutePath;
+}
+} // namespace
+
+ModuleFilesInfo::ModuleFilesInfo(PathRef MainFile,
+ const GlobalCompilationDatabase &CDB) {
+  std::optional PI = CDB.getProjectInfo(MainFile);
+  if (!PI)
+return;
+
+  llvm::SmallString<128> Result(PI->SourceRoot);
+  llvm::sys::path::append(Result, ".cache");
+  llvm::sys::path::append(Result, "clangd");
+  llvm::sys::path::append(Result, "module_files");
+  llvm::sys::fs::create_directories(Result, /*IgnoreExisting=*/true);
+
+  llvm::sys::path::append(Result, llvm::sys::path::filename(MainFile));
+  llvm::sys::fs::createUniqueDirectory(Result, UniqueModuleFilesPathPrefix);
+
+  log("Initialized module files to {0}", UniqueModuleFilesPathPrefix.str());
+}
+
+ModuleFilesInfo::~ModuleFilesInfo() {
+  DependentModuleNames.clear();
+  Successed = false;
+
+  if (UniqueModuleFilesPathPrefix.empty())
+return;
+
+  llvm::sys::fs::remove_directories(UniqueModuleFilesPathPrefix);
+  UniqueModuleFilesPathPrefix.clear();
+}
+
+llvm::SmallString<256>
+ModuleFilesInfo::getModuleFilePath(StringRef ModuleName) const {
+  llvm::SmallString<256> ModuleFilePath;
+
+  ModuleFilePath = UniqueModuleFilesPathPrefix;
+  auto [PrimaryModuleName, PartitionName] = ModuleName.split(':');
+  llvm::sys::path::append(ModuleFilePath, PrimaryModuleName);
+  if (!PartitionName.empty()) {
+ModuleFilePath.append("-");
+ModuleFilePath.append(PartitionName);
+  }
+  ModuleFilePath.append(".pcm");
+
+  return ModuleFilePath;
+}
+
+bool ModuleFilesInfo::IsModuleUnitBuilt(StringRef ModuleName) const {
+  if (!DependentModuleNames.count(ModuleName))
+return false;
+
+  auto BMIPath = getModuleFilePath(ModuleName);
+  if (llvm::sys::fs::exists(BMIPath))
+return true;
+
+  Successed = false;
+
+  DependentModuleNames.erase(ModuleName);
+  return false;
+}
+
+void ModuleFilesInfo::ReplaceHeaderSearchOptions(
+HeaderSearchOptions &Options) const {
+  if (!IsInited())
+return;
+
+  Options.PrebuiltModulePaths.insert(Options.PrebuiltModulePaths.begin(),
+ UniqueModuleFilesPathPrefix.str().str());
+
+  for (auto Iter = Options.PrebuiltModuleFiles.begin();
+   Iter != Options.PrebuiltModuleFiles.end();) {
+if (IsModuleUnitBuilt(Iter->first)) {
+  Iter = Options.PrebuiltModuleFiles.erase(Iter);
+  continue;
+}
+
+Iter++;
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(
+tooling::CompileCommand &Cmd) const {
+  if (!IsInited())
+return;
+
+  std::vector CommandLine(std::move(Cmd.CommandLine));
+
+  Cmd.CommandLine.emplace_back(CommandLine[0]);
+  Cmd.CommandLine.emplace_back(
+  llvm::Twine("-fprebuilt-module-path=" + UniqueModuleFilesPathPrefix)
+  .str());
+
+  for (std::size_t I = 1; I < CommandLine.size(); I++) {
+const std::string &Arg = CommandLine[I];
+const auto &[LHS, RHS] = StringRef(Arg).split("=");
+
+// Remove original `-fmodule-file==` form if it
+// already built.
+if (LHS == "-fmodule-file" && RHS.contains("=")) {
+  const auto &[ModuleName, _] = RHS.split("=");
+  if (IsModuleUnitBuilt(ModuleName))
+continue;
+}
+
+Cmd.CommandLine.emplace_back(Arg);
+  }
+}
+
+void ModuleFilesInfo::ReplaceCompileCommands(tooling::CompileCommand &Cmd,
+ StringRef OutputModuleName) const 
{
+  if (!IsInited())
+return;
+
+  ReplaceCompileCommands(Cmd);
+
+  Cmd.Output = getModuleFilePath(OutputModuleName).str().str();
+}
+
+bool ModuleFilesInfo::buildModuleFile(PathRef ModuleUnitFileName,
+  ModuleDependencyScanner &Scanner) {
+  if (ModuleUnitFileName.empty())
+return false;
+
+  for (auto &ModuleName : Scanner.getRequiredModules(ModuleUnitFileName)) {
+// Return early if there are errors building the module file.
+if (!IsModuleUnitBuilt(ModuleName) &&
+!buildModuleFile(Scanner.getSourceForModuleN

[clang] [clang] [MinGW] Explicitly always pass the -fno-use-init-array (PR #68571)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-driver


Changes

On MinGW targets, the .ctors section is always used for constructors.

Make sure that all layers of code generation is aware of this, wherever it 
matters, by passing the -fno-use-init-array option, setting the TargetOptions 
field UseInitArray to false.

This fixes https://github.com/llvm/llvm-project/issues/55938.

---
Full diff: https://github.com/llvm/llvm-project/pull/68571.diff


2 Files Affected:

- (modified) clang/lib/Driver/ToolChains/MinGW.cpp (+2) 
- (modified) clang/test/Driver/mingw.cpp (+3) 


``diff
diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp 
b/clang/lib/Driver/ToolChains/MinGW.cpp
index 5872e13bda358f8..d3d829a8ddbdb95 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -709,6 +709,8 @@ void toolchains::MinGW::addClangTargetOptions(
 }
   }
 
+  CC1Args.push_back("-fno-use-init-array");
+
   for (auto Opt : {options::OPT_mthreads, options::OPT_mwindows,
options::OPT_mconsole, options::OPT_mdll}) {
 if (Arg *A = DriverArgs.getLastArgNoClaim(Opt))
diff --git a/clang/test/Driver/mingw.cpp b/clang/test/Driver/mingw.cpp
index 0f276820d0fff22..bb22a0652b486e1 100644
--- a/clang/test/Driver/mingw.cpp
+++ b/clang/test/Driver/mingw.cpp
@@ -77,3 +77,6 @@
 // CHECK_NO_SUBSYS-NOT: "--subsystem"
 // CHECK_SUBSYS_CONSOLE: "--subsystem" "console"
 // CHECK_SUBSYS_WINDOWS: "--subsystem" "windows"
+
+// RUN: %clang -target i686-windows-gnu -### %s 2>&1 | FileCheck 
-check-prefix=CHECK_NO_INIT_ARRAY %s
+// CHECK_NO_INIT_ARRAY: "-fno-use-init-array"

``




https://github.com/llvm/llvm-project/pull/68571
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] [MinGW] Explicitly always pass the -fno-use-init-array (PR #68571)

2023-10-09 Thread Martin Storsjö via cfe-commits

mstorsjo wrote:

This is an alternative to #68570. This has the upside that the `UseInitArray` 
flag is set to the correct value throughout the chain, for any other potential 
users of the field, in case it would affect code generation in other places. 
The downside is that if we only go with this, then any other language frontend, 
which might not know about the details about `.ctors`, will end up with the 
same bug unless they also add a similar fix. (We could of course go with both 
fixes as well.)

https://github.com/llvm/llvm-project/pull/68571
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Chuanqi Xu via cfe-commits

ChuanqiXu9 wrote:

@sam-mccall thanks  for your high qualified comments! I think I've addressed 
most of them. I am not so familiar with the Github review system. Tell me if 
you're more happy with other practices.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Fix `ArrayInitLoopExpr` handling (PR #67886)

2023-10-09 Thread via cfe-commits

isuckatcs wrote:

ping @tbaederr 

https://github.com/llvm/llvm-project/pull/67886
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer] Compute length of string literal initializers (#66990) (PR #68368)

2023-10-09 Thread via cfe-commits

https://github.com/DonatNagyE edited 
https://github.com/llvm/llvm-project/pull/68368
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer] Compute length of string literal initializers (#66990) (PR #68368)

2023-10-09 Thread via cfe-commits

https://github.com/DonatNagyE requested changes to this pull request.

As far as I see the code that you added doesn't check whether the global 
variable is a constant or not. Please add a check for this (examine the type of 
the declaration `decl`) and consider adding test cases which validate that this 
logic doesn't assign length values to non-constant global strings (because 
their actual content may be different from the string literal that was used to 
initialize them).

Also note that in general, the LLVM coding style uses CamelCased names for 
variables; however here I can accept lowerCamelCased names if you want to 
remain consistent with the surrounding legacy code.

https://github.com/llvm/llvm-project/pull/68368
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer] Compute length of string literal initializers (#66990) (PR #68368)

2023-10-09 Thread via cfe-commits


@@ -930,9 +930,24 @@ SVal CStringChecker::getCStringLength(CheckerContext &C, 
ProgramStateRef &state,
 const StringLiteral *strLit = cast(MR)->getStringLiteral();
 return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
   }
+  case MemRegion::NonParamVarRegionKind: {
+// If we have a global constant with a string literal initializer,
+// compute the initializer's length.
+const VarDecl *decl = cast(MR)->getDecl();
+if (decl->hasGlobalStorage()) {
+  if (const Expr *init = decl->getInit()) {
+if (auto *strLit = dyn_cast(init)) {
+  SValBuilder &svalBuilder = C.getSValBuilder();
+  QualType sizeTy = svalBuilder.getContext().getSizeType();
+  return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
+}
+  }
+}
+// Otherwise, fallback to this.
+return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);

DonatNagyE wrote:

```suggestion
[[fallthrough]];
```
Use this convention (which is technically an annotated empty statement) to 
create a clean fallthrough instead of duplicating the logic of the other 
branches.

https://github.com/llvm/llvm-project/pull/68368
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer] Compute length of string literal initializers (#66990) (PR #68368)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

Fix issue https://github.com/llvm/llvm-project/issues/66990

---
Full diff: https://github.com/llvm/llvm-project/pull/68368.diff


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (+16-1) 
- (modified) clang/test/Analysis/string.c (+11) 


``diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
index f1539f2733298d9..b3b15c35450acdc 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
@@ -930,9 +930,24 @@ SVal CStringChecker::getCStringLength(CheckerContext &C, 
ProgramStateRef &state,
 const StringLiteral *strLit = cast(MR)->getStringLiteral();
 return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
   }
+  case MemRegion::NonParamVarRegionKind: {
+// If we have a global constant with a string literal initializer,
+// compute the initializer's length.
+const VarDecl *decl = cast(MR)->getDecl();
+if (decl->hasGlobalStorage()) {
+  if (const Expr *init = decl->getInit()) {
+if (auto *strLit = dyn_cast(init)) {
+  SValBuilder &svalBuilder = C.getSValBuilder();
+  QualType sizeTy = svalBuilder.getContext().getSizeType();
+  return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
+}
+  }
+}
+// Otherwise, fallback to this.
+return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
+  }
   case MemRegion::SymbolicRegionKind:
   case MemRegion::AllocaRegionKind:
-  case MemRegion::NonParamVarRegionKind:
   case MemRegion::ParamVarRegionKind:
   case MemRegion::FieldRegionKind:
   case MemRegion::ObjCIvarRegionKind:
diff --git a/clang/test/Analysis/string.c b/clang/test/Analysis/string.c
index d369ee9f7d854a1..8da905b33145562 100644
--- a/clang/test/Analysis/string.c
+++ b/clang/test/Analysis/string.c
@@ -97,6 +97,17 @@ void strlen_constant2(char x) {
   clang_analyzer_eval(strlen(a) == 3); // expected-warning{{UNKNOWN}}
 }
 
+const char *const global_str_ptr = "abcd";
+const char global_str_arr[] = "efgh";
+
+void strlen_global_constant_ptr(void) {
+  clang_analyzer_eval(strlen(global_str_ptr) == 4); // expected-warning{{TRUE}}
+}
+
+void strlen_global_constant_arr(void) {
+  clang_analyzer_eval(strlen(global_str_arr) == 4); // expected-warning{{TRUE}}
+}
+
 size_t strlen_null(void) {
   return strlen(0); // expected-warning{{Null pointer passed as 1st argument 
to string length function}}
 }

``




https://github.com/llvm/llvm-project/pull/68368
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reapply "[clang analysis][thread-safety] Handle return-by-reference..… (PR #68572)

2023-10-09 Thread Clement Courbet via cfe-commits

https://github.com/legrosbuffle created 
https://github.com/llvm/llvm-project/pull/68572

…… (#68394)"

The new warnings are now under a separate flag 
`-Wthread-safety-reference-return`. The plan is:

- Start with a period where people can opt into the new warnings with 
`-Wthread-safety-reference-return` or `-Wthread-safety-beta`. This allows 
downstream to test the new warnings without having to roll the implementation 
back and forth.
- Make `-Wthread-safety-reference-return` part of `-Wthread-safety-reference`. 
People can opt out via `-Wthread-safety-reference 
-Wnothread-safety-reference-return`.
- (maybe) delete `-Wthread-safety-reference-return` after some time ?

This reverts commit 859f2d032386632562521a99db20923217d98988.

>From a801efe3a6799df6ecee7ddf1ce77572d756cabb Mon Sep 17 00:00:00 2001
From: Clement Courbet 
Date: Mon, 9 Oct 2023 10:20:12 +0200
Subject: [PATCH] =?UTF-8?q?Reapply=20"[clang=20analysis][thread-safety]=20?=
 =?UTF-8?q?Handle=20return-by-reference..=E2=80=A6=20(#68394)"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The new warnings are now under a separate flag 
`-Wthread-safety-reference-return`. The plan is:

- Start with a period where people can opt into the new warnings with 
`-Wthread-safety-reference-return` or `-Wthread-safety-beta`. This allows 
downstream to test the new warnings without having to roll the implementation 
back and forth.
- Make `-Wthread-safety-reference-return` part of `-Wthread-safety-reference`. 
People can opt out via `-Wthread-safety-reference 
-Wnothread-safety-reference-return`.
- (maybe) delete `-Wthread-safety-reference-return` after some time ?

This reverts commit 859f2d032386632562521a99db20923217d98988.
---
 .../clang/Analysis/Analyses/ThreadSafety.h|  8 +-
 clang/include/clang/Basic/DiagnosticGroups.td | 14 ++--
 .../clang/Basic/DiagnosticSemaKinds.td| 10 ++-
 clang/lib/Analysis/ThreadSafety.cpp   | 80 +--
 clang/lib/Sema/AnalysisBasedWarnings.cpp  | 12 +++
 .../SemaCXX/warn-thread-safety-analysis.cpp   | 79 ++
 6 files changed, 169 insertions(+), 34 deletions(-)

diff --git a/clang/include/clang/Analysis/Analyses/ThreadSafety.h 
b/clang/include/clang/Analysis/Analyses/ThreadSafety.h
index 1808d1d71e05d2c..0866b09bab2995e 100644
--- a/clang/include/clang/Analysis/Analyses/ThreadSafety.h
+++ b/clang/include/clang/Analysis/Analyses/ThreadSafety.h
@@ -47,7 +47,13 @@ enum ProtectedOperationKind {
   POK_PassByRef,
 
   /// Passing a pt-guarded variable by reference.
-  POK_PtPassByRef
+  POK_PtPassByRef,
+
+  /// Returning a guarded variable by reference.
+  POK_ReturnByRef,
+
+  /// Returning a pt-guarded variable by reference.
+  POK_PtReturnByRef,
 };
 
 /// This enum distinguishes between different kinds of lock actions. For
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 0b09c002191848a..0462919af660285 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1061,18 +1061,20 @@ def Most : DiagGroup<"most", [
  ]>;
 
 // Thread Safety warnings
-def ThreadSafetyAttributes : DiagGroup<"thread-safety-attributes">;
-def ThreadSafetyAnalysis   : DiagGroup<"thread-safety-analysis">;
-def ThreadSafetyPrecise: DiagGroup<"thread-safety-precise">;
-def ThreadSafetyReference  : DiagGroup<"thread-safety-reference">;
-def ThreadSafetyNegative   : DiagGroup<"thread-safety-negative">;
+def ThreadSafetyAttributes   : DiagGroup<"thread-safety-attributes">;
+def ThreadSafetyAnalysis : DiagGroup<"thread-safety-analysis">;
+def ThreadSafetyPrecise  : DiagGroup<"thread-safety-precise">;
+def ThreadSafetyReference: DiagGroup<"thread-safety-reference">;
+def ThreadSafetyReferenceReturn  : DiagGroup<"thread-safety-reference-return">;
+def ThreadSafetyNegative : DiagGroup<"thread-safety-negative">;
 def ThreadSafety : DiagGroup<"thread-safety",
  [ThreadSafetyAttributes,
   ThreadSafetyAnalysis,
   ThreadSafetyPrecise,
   ThreadSafetyReference]>;
 def ThreadSafetyVerbose : DiagGroup<"thread-safety-verbose">;
-def ThreadSafetyBeta : DiagGroup<"thread-safety-beta">;
+def ThreadSafetyBeta : DiagGroup<"thread-safety-beta",
+ [ThreadSafetyReferenceReturn]>;
 
 // Uniqueness Analysis warnings
 def Consumed   : DiagGroup<"consumed">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index b211680a0e9b6e9..c777d73dd4a769b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3864,7 +3864,7 @@ def warn_fun_requires_negative_cap : Warning<
   "calling function %0 requires negative capability '%1'">,
   InGroup, DefaultIgnore;
 
-//

[libunwind] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan edited 
https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits


@@ -1917,8 +1917,13 @@ class OpenMPIRBuilder {
   ///
   /// \param Loc The location where the teams construct was encountered.
   /// \param BodyGenCB Callback that will generate the region code.
+  /// \param NumTeamsUpper Upper bound on the number of teams.
+  /// \param ThreadLimit on the number of threads that may participate in a
+  ///contention group created by each team.
   InsertPointTy createTeams(const LocationDescription &Loc,
-BodyGenCallbackTy BodyGenCB);
+BodyGenCallbackTy BodyGenCB,
+Value *NumTeamsUpper = nullptr,

kiranchandramohan wrote:

What about the LowerBound?

https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan commented:

Is this change for OMP 5.0 and before?

https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits


@@ -1917,8 +1917,13 @@ class OpenMPIRBuilder {
   ///
   /// \param Loc The location where the teams construct was encountered.
   /// \param BodyGenCB Callback that will generate the region code.
+  /// \param NumTeamsUpper Upper bound on the number of teams.
+  /// \param ThreadLimit on the number of threads that may participate in a
+  ///contention group created by each team.
   InsertPointTy createTeams(const LocationDescription &Loc,
-BodyGenCallbackTy BodyGenCB);
+BodyGenCallbackTy BodyGenCB,
+Value *NumTeamsUpper = nullptr,

kiranchandramohan wrote:

What about the LowerBound?

https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan commented:

Is this change for OMP 5.0 and before?

https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [OpenMPIRBuilder] Add ThreadLimit and NumTeamsUpper clauses to teams clause (PR #68364)

2023-10-09 Thread Kiran Chandramohan via cfe-commits

https://github.com/kiranchandramohan edited 
https://github.com/llvm/llvm-project/pull/68364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reapply "[clang analysis][thread-safety] Handle return-by-reference..… (PR #68572)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

…… (#68394)"

The new warnings are now under a separate flag 
`-Wthread-safety-reference-return`. The plan is:

- Start with a period where people can opt into the new warnings with 
`-Wthread-safety-reference-return` or `-Wthread-safety-beta`. This allows 
downstream to test the new warnings without having to roll the implementation 
back and forth.
- Make `-Wthread-safety-reference-return` part of `-Wthread-safety-reference`. 
People can opt out via `-Wthread-safety-reference 
-Wnothread-safety-reference-return`.
- (maybe) delete `-Wthread-safety-reference-return` after some time ?

This reverts commit 859f2d032386632562521a99db20923217d98988.

---
Full diff: https://github.com/llvm/llvm-project/pull/68572.diff


6 Files Affected:

- (modified) clang/include/clang/Analysis/Analyses/ThreadSafety.h (+7-1) 
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+8-6) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+9-1) 
- (modified) clang/lib/Analysis/ThreadSafety.cpp (+54-26) 
- (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+12) 
- (modified) clang/test/SemaCXX/warn-thread-safety-analysis.cpp (+79) 


``diff
diff --git a/clang/include/clang/Analysis/Analyses/ThreadSafety.h 
b/clang/include/clang/Analysis/Analyses/ThreadSafety.h
index 1808d1d71e05d2c..0866b09bab2995e 100644
--- a/clang/include/clang/Analysis/Analyses/ThreadSafety.h
+++ b/clang/include/clang/Analysis/Analyses/ThreadSafety.h
@@ -47,7 +47,13 @@ enum ProtectedOperationKind {
   POK_PassByRef,
 
   /// Passing a pt-guarded variable by reference.
-  POK_PtPassByRef
+  POK_PtPassByRef,
+
+  /// Returning a guarded variable by reference.
+  POK_ReturnByRef,
+
+  /// Returning a pt-guarded variable by reference.
+  POK_PtReturnByRef,
 };
 
 /// This enum distinguishes between different kinds of lock actions. For
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 0b09c002191848a..0462919af660285 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1061,18 +1061,20 @@ def Most : DiagGroup<"most", [
  ]>;
 
 // Thread Safety warnings
-def ThreadSafetyAttributes : DiagGroup<"thread-safety-attributes">;
-def ThreadSafetyAnalysis   : DiagGroup<"thread-safety-analysis">;
-def ThreadSafetyPrecise: DiagGroup<"thread-safety-precise">;
-def ThreadSafetyReference  : DiagGroup<"thread-safety-reference">;
-def ThreadSafetyNegative   : DiagGroup<"thread-safety-negative">;
+def ThreadSafetyAttributes   : DiagGroup<"thread-safety-attributes">;
+def ThreadSafetyAnalysis : DiagGroup<"thread-safety-analysis">;
+def ThreadSafetyPrecise  : DiagGroup<"thread-safety-precise">;
+def ThreadSafetyReference: DiagGroup<"thread-safety-reference">;
+def ThreadSafetyReferenceReturn  : DiagGroup<"thread-safety-reference-return">;
+def ThreadSafetyNegative : DiagGroup<"thread-safety-negative">;
 def ThreadSafety : DiagGroup<"thread-safety",
  [ThreadSafetyAttributes,
   ThreadSafetyAnalysis,
   ThreadSafetyPrecise,
   ThreadSafetyReference]>;
 def ThreadSafetyVerbose : DiagGroup<"thread-safety-verbose">;
-def ThreadSafetyBeta : DiagGroup<"thread-safety-beta">;
+def ThreadSafetyBeta : DiagGroup<"thread-safety-beta",
+ [ThreadSafetyReferenceReturn]>;
 
 // Uniqueness Analysis warnings
 def Consumed   : DiagGroup<"consumed">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index b211680a0e9b6e9..c777d73dd4a769b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3864,7 +3864,7 @@ def warn_fun_requires_negative_cap : Warning<
   "calling function %0 requires negative capability '%1'">,
   InGroup, DefaultIgnore;
 
-// Thread safety warnings on pass by reference
+// Thread safety warnings on pass/return by reference
 def warn_guarded_pass_by_reference : Warning<
   "passing variable %1 by reference requires holding %0 "
   "%select{'%2'|'%2' exclusively}3">,
@@ -3873,6 +3873,14 @@ def warn_pt_guarded_pass_by_reference : Warning<
   "passing the value that %1 points to by reference requires holding %0 "
   "%select{'%2'|'%2' exclusively}3">,
   InGroup, DefaultIgnore;
+def warn_guarded_return_by_reference : Warning<
+  "returning variable %1 by reference requires holding %0 "
+  "%select{'%2'|'%2' exclusively}3">,
+  InGroup, DefaultIgnore;
+def warn_pt_guarded_return_by_reference : Warning<
+  "returning the value that %1 points to by reference requires holding %0 "
+  "%select{'%2'|'%2' exclusively}3">,
+  InGroup, DefaultIgnore;
 
 // Imprecise thread safety warnings
 def warn_variable_requires_lock : Warning<
diff --git a/clang/lib/Analysis/ThreadSafet

[clang] Reapply "[clang analysis][thread-safety] Handle return-by-reference..… (PR #68572)

2023-10-09 Thread Clement Courbet via cfe-commits

legrosbuffle wrote:

@aeubanks FYI

https://github.com/llvm/llvm-project/pull/68572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][dataflow] Various formatting tweaks to HTMLLogger. (PR #68392)

2023-10-09 Thread via cfe-commits

https://github.com/martinboehme edited 
https://github.com/llvm/llvm-project/pull/68392
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][dataflow] Various formatting tweaks to HTMLLogger. (PR #68392)

2023-10-09 Thread Sam McCall via cfe-commits

https://github.com/sam-mccall approved this pull request.


https://github.com/llvm/llvm-project/pull/68392
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Handle delegating constructors (PR #67823)

2023-10-09 Thread Timm Baeder via cfe-commits

tbaederr wrote:

Ping

https://github.com/llvm/llvm-project/pull/67823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Handle variadic functions (PR #67814)

2023-10-09 Thread Timm Baeder via cfe-commits

tbaederr wrote:

Ping

https://github.com/llvm/llvm-project/pull/67814
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Fix returning nullptr from functions (PR #67229)

2023-10-09 Thread Timm Baeder via cfe-commits

tbaederr wrote:

Ping

https://github.com/llvm/llvm-project/pull/67229
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Handle CXXScalarValueInitExprs (PR #67147)

2023-10-09 Thread Timm Baeder via cfe-commits

tbaederr wrote:

Ping

https://github.com/llvm/llvm-project/pull/67147
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= 
Message-ID:
In-Reply-To: 


https://github.com/DonatNagyE edited 
https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= 
Message-ID:
In-Reply-To: 



@@ -84,33 +104,70 @@ class InvalidPtrChecker
 REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)
 
 // Stores the region of the environment pointer of 'main' (if present).
-REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const MemRegion *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(MainEnvPtrRegion, const MemRegion *)
+
+// Stores the regions of environments returned by getenv calls.
+REGISTER_SET_WITH_PROGRAMSTATE(GetenvEnvPtrRegions, const MemRegion *)
 
 // Stores key-value pairs, where key is function declaration and value is
 // pointer to memory region returned by previous call of this function
 REGISTER_MAP_WITH_PROGRAMSTATE(PreviousCallResultMap, const FunctionDecl *,
const MemRegion *)
 
+const NoteTag *InvalidPtrChecker::createEnvInvalidationNote(
+CheckerContext &C, ProgramStateRef State, StringRef FunctionName) const {
+
+  const MemRegion *MainRegion = State->get();
+  const auto GetenvRegions = State->get();
+
+  return C.getNoteTag([this, MainRegion, GetenvRegions,
+   FunctionName = std::string{FunctionName}](
+  PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
+auto IsInterestingForInvalidation = [this, &BR](const MemRegion *R) {
+  return R && &BR.getBugType() == &InvalidPtrBugType && 
BR.isInteresting(R);
+};
+
+// Craft the note tag message.
+llvm::SmallVector InvalidLocationNames;
+if (IsInterestingForInvalidation(MainRegion)) {
+  InvalidLocationNames.push_back("the environment parameter of 'main'");
+}
+if (llvm::any_of(GetenvRegions, IsInterestingForInvalidation))
+  InvalidLocationNames.push_back("the environment returned by 'getenv'");
+
+if (InvalidLocationNames.size() >= 1)
+  Out << '\'' << FunctionName << "' call may invalidate "
+  << InvalidLocationNames[0];
+if (InvalidLocationNames.size() == 2)
+  Out << ", and " << InvalidLocationNames[1];
+
+// Mark all regions that were interesting before as NOT interesting now
+// to avoid extra notes coming from invalidation points higher up the
+// bugpath. This ensures, that only the last invalidation point is marked
+// with a note tag.
+if (IsInterestingForInvalidation(MainRegion))
+  BR.markNotInteresting(MainRegion);
+for (const MemRegion *GetenvRegion : GetenvRegions)
+  if (IsInterestingForInvalidation(GetenvRegion))
+BR.markNotInteresting(GetenvRegion);

DonatNagyE wrote:

You can merge this check-and-loop with the check-and-loop (more precisely 
check-and-`any_of`) that constructs `InvalidLocationNames` to avoid unnecessary 
repeated calls to `IsInterestingForInvalidation`.

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= 
Message-ID:
In-Reply-To: 



@@ -84,33 +104,70 @@ class InvalidPtrChecker
 REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)
 
 // Stores the region of the environment pointer of 'main' (if present).
-REGISTER_TRAIT_WITH_PROGRAMSTATE(EnvPtrRegion, const MemRegion *)
+REGISTER_TRAIT_WITH_PROGRAMSTATE(MainEnvPtrRegion, const MemRegion *)
+
+// Stores the regions of environments returned by getenv calls.
+REGISTER_SET_WITH_PROGRAMSTATE(GetenvEnvPtrRegions, const MemRegion *)
 
 // Stores key-value pairs, where key is function declaration and value is
 // pointer to memory region returned by previous call of this function
 REGISTER_MAP_WITH_PROGRAMSTATE(PreviousCallResultMap, const FunctionDecl *,
const MemRegion *)
 
+const NoteTag *InvalidPtrChecker::createEnvInvalidationNote(
+CheckerContext &C, ProgramStateRef State, StringRef FunctionName) const {
+
+  const MemRegion *MainRegion = State->get();
+  const auto GetenvRegions = State->get();
+
+  return C.getNoteTag([this, MainRegion, GetenvRegions,
+   FunctionName = std::string{FunctionName}](
+  PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
+auto IsInterestingForInvalidation = [this, &BR](const MemRegion *R) {
+  return R && &BR.getBugType() == &InvalidPtrBugType && 
BR.isInteresting(R);
+};

DonatNagyE wrote:

```suggestion
if (&BR.getBugType() != &InvalidPtrBugType);
  return;
auto IsInterestingForInvalidation = [&BR](const MemRegion *R) {
  return R && BR.isInteresting(R);
};
```
Perform an early return for unrelated bugs instead of capturing `this` in the 
inner lambda!

In fact, after this simplification you can just eliminate the the inner lambda 
by inlining its definition. (I'd guess that `GetenvRegions` doesn't contain 
nullpointers, so there you can omit the `R &&` check.)

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= ,
Endre =?utf-8?q?F=C3=BCl=C3=B6p?= 
Message-ID:
In-Reply-To: 


https://github.com/DonatNagyE commented:

Thanks for improving this patch!

I found yet another opportunity to kill some lambda functions ;)

https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= 
Message-ID:
In-Reply-To: 


https://github.com/DonatNagyE edited 
https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][clangsa] Add new option to alpha.security.cert.InvalidPtrChecker (PR #67663)

2023-10-09 Thread via cfe-commits
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= ,
Endre =?utf-8?q?Fülöp?= 
Message-ID:
In-Reply-To: 


https://github.com/DonatNagyE edited 
https://github.com/llvm/llvm-project/pull/67663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] add namespace qualifier NFC (PR #68579)

2023-10-09 Thread Mikhail Goncharov via cfe-commits

https://github.com/metaflow created 
https://github.com/llvm/llvm-project/pull/68579

for daca97216cf132d733513f992d49e3c722aabf40 #68134 adds a namespace as we are 
not using llvm::StringRef yet

>From c91b8034ddcd97fb255ccafe7a153056c0c2f446 Mon Sep 17 00:00:00 2001
From: Mikhail Goncharov 
Date: Mon, 9 Oct 2023 13:52:27 +0200
Subject: [PATCH] [clang-tidy] add namespace qualifier NFC

for daca97216cf132d733513f992d49e3c722aabf40 #68134 adds a namespace
as we are not using llvm::StringRef yet
---
 clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h 
b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
index 31fcdaa7b273b0b..7d4120085b86677 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
+++ b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
@@ -10,7 +10,7 @@
 
 namespace clang::tidy::llvm_libc {
 
-const static StringRef RequiredNamespaceStart = "__llvm_libc";
-const static StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
+const static llvm::StringRef RequiredNamespaceStart = "__llvm_libc";
+const static llvm::StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
 
 } // namespace clang::tidy::llvm_libc

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] add namespace qualifier NFC (PR #68579)

2023-10-09 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy


Changes

for daca97216cf132d733513f992d49e3c722aabf40 #68134 adds a namespace as 
we are not using llvm::StringRef yet

---
Full diff: https://github.com/llvm/llvm-project/pull/68579.diff


1 Files Affected:

- (modified) clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h (+2-2) 


``diff
diff --git a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h 
b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
index 31fcdaa7b273b0b..7d4120085b86677 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
+++ b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
@@ -10,7 +10,7 @@
 
 namespace clang::tidy::llvm_libc {
 
-const static StringRef RequiredNamespaceStart = "__llvm_libc";
-const static StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
+const static llvm::StringRef RequiredNamespaceStart = "__llvm_libc";
+const static llvm::StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
 
 } // namespace clang::tidy::llvm_libc

``




https://github.com/llvm/llvm-project/pull/68579
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 2856e72 - [analyzer][NFC] Remove outdated FIXME comment (#68211)

2023-10-09 Thread via cfe-commits

Author: DonatNagyE
Date: 2023-10-09T13:56:20+02:00
New Revision: 2856e729f3640c500499a84fb741b15775e529cf

URL: 
https://github.com/llvm/llvm-project/commit/2856e729f3640c500499a84fb741b15775e529cf
DIFF: 
https://github.com/llvm/llvm-project/commit/2856e729f3640c500499a84fb741b15775e529cf.diff

LOG: [analyzer][NFC] Remove outdated FIXME comment (#68211)

This trivial commit removes a 13-year-old FIXME comment from
MallocChecker.cpp because it was fixed at least 10 years ago when
`getConjuredHeapSymbolVal()` was added.

Added: 


Modified: 
clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

Removed: 




diff  --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index 18c7f3e4f6e6b92..d3a4020280616b0 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -1961,13 +1961,10 @@ ProgramStateRef MallocChecker::FreeMemAux(
   // Parameters, locals, statics, globals, and memory returned by
   // __builtin_alloca() shouldn't be freed.
   if (!isa(MS)) {
-// FIXME: at the time this code was written, malloc() regions were
-// represented by conjured symbols, which are all in UnknownSpaceRegion.
-// This means that there isn't actually anything from HeapSpaceRegion
-// that should be freed, even though we allow it here.
-// Of course, free() can work on memory allocated outside the current
-// function, so UnknownSpaceRegion is always a possibility.
-// False negatives are better than false positives.
+// Regions returned by malloc() are represented by SymbolicRegion objects
+// within HeapSpaceRegion. Of course, free() can work on memory allocated
+// outside the current function, so UnknownSpaceRegion is also a
+// possibility here.
 
 if (isa(R))
   HandleFreeAlloca(C, ArgVal, ArgExpr->getSourceRange());



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [analyzer][NFC] Remove outdated FIXME comment (PR #68211)

2023-10-09 Thread via cfe-commits

https://github.com/DonatNagyE closed 
https://github.com/llvm/llvm-project/pull/68211
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Ben Boeckel via cfe-commits


@@ -45,6 +45,10 @@ class GlobalCompilationDatabase {
 return std::nullopt;
   }
 
+  virtual std::vector getAllFilesInProjectOf(PathRef File) const {

mathstuf wrote:

Probably easiest to see it by the writer code 
[here](https://gitlab.kitware.com/ben.boeckel/cmake/-/commit/9866da328034fbb3a0e96f5cca81e913c0ab8678#b94b7587efb540b6ad866ae5620492d78f6cff24_0_23).
 Basically, it is:

```json
{
  "version": 1,
  "revision": 0,
  "module_sets": [
{
  "name": "unique name of module set",
  "visible_module_sets": [
"module sets",
"which can satisfy",
"required modules within this module set",
  ],
  "modules": [
"work-directory": "/path/to/working/directory",
"source": "path/to/source/file",
"name": "name.of.provided.module",
"private": false,
"requires": [
  "list",
  "of",
  "required",
  "modulest"
],
"flag_sets": [
  "-fstandalone-flag",
  ["-fflag", "with-arg"],
]
  ]
}
  ]
}
```

The semantics are:

- module sets may refer to other module sets named in order to satisfy their 
required modules;
- however, private modules in other module sets are not usable;
- `work-directory` is optional;
- `private` defaults to `false`;
- flag sets need to be used to compile the BMI in question (Daniel Ruoso's 
"local preprocessor arguments");
- flags may be deduplicated/merged by top-level identities.

https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] 60f7aa1 - [clang-tidy] add namespace qualifier NFC (#68579)

2023-10-09 Thread via cfe-commits

Author: Mikhail Goncharov
Date: 2023-10-09T13:58:13+02:00
New Revision: 60f7aa1a6ccb4214aec8838096abbdc1b37169a5

URL: 
https://github.com/llvm/llvm-project/commit/60f7aa1a6ccb4214aec8838096abbdc1b37169a5
DIFF: 
https://github.com/llvm/llvm-project/commit/60f7aa1a6ccb4214aec8838096abbdc1b37169a5.diff

LOG: [clang-tidy] add namespace qualifier NFC (#68579)

for daca97216cf132d733513f992d49e3c722aabf40 #68134 adds a namespace as
we are not using llvm::StringRef yet

Added: 


Modified: 
clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h 
b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
index 31fcdaa7b273b0b..7d4120085b86677 100644
--- a/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
+++ b/clang-tools-extra/clang-tidy/llvmlibc/NamespaceConstants.h
@@ -10,7 +10,7 @@
 
 namespace clang::tidy::llvm_libc {
 
-const static StringRef RequiredNamespaceStart = "__llvm_libc";
-const static StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
+const static llvm::StringRef RequiredNamespaceStart = "__llvm_libc";
+const static llvm::StringRef RequiredNamespaceMacroName = "LIBC_NAMESPACE";
 
 } // namespace clang::tidy::llvm_libc



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] add namespace qualifier NFC (PR #68579)

2023-10-09 Thread Mikhail Goncharov via cfe-commits

https://github.com/metaflow closed 
https://github.com/llvm/llvm-project/pull/68579
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Ben Boeckel via cfe-commits

https://github.com/mathstuf edited 
https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Diagnose problematic uses of constructor/destructor attribute (PR #67673)

2023-10-09 Thread Aaron Ballman via cfe-commits


@@ -2352,26 +2352,126 @@ static void handleUnusedAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
   D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
 }
 
-static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
-  uint32_t priority = ConstructorAttr::DefaultPriority;
+static void diagnoseInvalidPriority(Sema &S, uint32_t Priority,
+const ParsedAttr &A,
+SourceLocation PriorityLoc) {
+  constexpr uint32_t ReservedPriorityLower = 101, ReservedPriorityUpper = 
65535;
+
+  // Only perform the priority check if the attribute is outside of a system
+  // header. Values <= 100 are reserved for the implementation, and libc++
+  // benefits from being able to specify values in that range. Values > 65535
+  // are reserved for historical reasons.
+  if ((Priority < ReservedPriorityLower || Priority > ReservedPriorityUpper) &&
+  !S.getSourceManager().isInSystemHeader(A.getLoc())) {
+S.Diag(A.getLoc(), diag::warn_priority_out_of_range)
+<< PriorityLoc << A << ReservedPriorityLower << ReservedPriorityUpper;
+  }
+}
+
+static bool FunctionParamsAreMainLike(ASTContext &Context,
+  const FunctionDecl *FD) {
+  assert(FD->hasPrototype() && "expected the function to have a prototype");
+  const auto *FPT = FD->getType()->castAs();
+  QualType CharPP =
+  Context.getPointerType(Context.getPointerType(Context.CharTy));
+  QualType Expected[] = {Context.IntTy, CharPP, CharPP, CharPP};
+
+  for (unsigned I = 0; I < FPT->getNumParams(); ++I) {

AaronBallman wrote:

Sure!

https://github.com/llvm/llvm-project/pull/67673
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Diagnose problematic uses of constructor/destructor attribute (PR #67673)

2023-10-09 Thread Aaron Ballman via cfe-commits


@@ -2352,26 +2352,126 @@ static void handleUnusedAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
   D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
 }
 
-static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
-  uint32_t priority = ConstructorAttr::DefaultPriority;
+static void diagnoseInvalidPriority(Sema &S, uint32_t Priority,
+const ParsedAttr &A,
+SourceLocation PriorityLoc) {
+  constexpr uint32_t ReservedPriorityLower = 101, ReservedPriorityUpper = 
65535;
+
+  // Only perform the priority check if the attribute is outside of a system
+  // header. Values <= 100 are reserved for the implementation, and libc++
+  // benefits from being able to specify values in that range. Values > 65535
+  // are reserved for historical reasons.
+  if ((Priority < ReservedPriorityLower || Priority > ReservedPriorityUpper) &&
+  !S.getSourceManager().isInSystemHeader(A.getLoc())) {
+S.Diag(A.getLoc(), diag::warn_priority_out_of_range)
+<< PriorityLoc << A << ReservedPriorityLower << ReservedPriorityUpper;
+  }
+}
+
+static bool FunctionParamsAreMainLike(ASTContext &Context,
+  const FunctionDecl *FD) {
+  assert(FD->hasPrototype() && "expected the function to have a prototype");
+  const auto *FPT = FD->getType()->castAs();
+  QualType CharPP =
+  Context.getPointerType(Context.getPointerType(Context.CharTy));
+  QualType Expected[] = {Context.IntTy, CharPP, CharPP, CharPP};
+
+  for (unsigned I = 0; I < FPT->getNumParams(); ++I) {
+QualType AT = FPT->getParamType(I);
+
+bool Mismatch = true;
+if (Context.hasSameUnqualifiedType(AT, Expected[I]))
+  Mismatch = false;
+else if (Expected[I] == CharPP) {
+  // As an extension, the following forms are okay:
+  //   char const **
+  //   char const * const *
+  //   char * const *
+
+  QualifierCollector Qs;
+  const PointerType *PT;
+  if ((PT = Qs.strip(AT)->getAs()) &&
+  (PT = Qs.strip(PT->getPointeeType())->getAs()) &&
+  Context.hasSameType(QualType(Qs.strip(PT->getPointeeType()), 0),
+  Context.CharTy)) {
+Qs.removeConst();
+Mismatch = !Qs.empty();
+  }
+}
+
+if (Mismatch)
+  return false;

AaronBallman wrote:

Good call, I've simplified.

https://github.com/llvm/llvm-project/pull/67673
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Ben Boeckel via cfe-commits

https://github.com/mathstuf edited 
https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] [C++20] [Modules] Introduce initial support for C++20 Modules (PR #66462)

2023-10-09 Thread Ben Boeckel via cfe-commits

https://github.com/mathstuf edited 
https://github.com/llvm/llvm-project/pull/66462
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Diagnose problematic uses of constructor/destructor attribute (PR #67673)

2023-10-09 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman updated 
https://github.com/llvm/llvm-project/pull/67673

>From 02565df450927fac88062f526a167c9beb518c2f Mon Sep 17 00:00:00 2001
From: Aaron Ballman 
Date: Thu, 28 Sep 2023 09:20:12 -0400
Subject: [PATCH 1/5] Diagnose problematic uses of constructor/destructor
 attribute

Functions with these attributes will be automatically called before
main() or after main() exits gracefully. In glibc environments, the
constructor function is passed the same arguments as main(), so that
signature is allowed. In all other environments, we require the
function to accept no arguments and either return `void` or `int`. The
functions must use the C calling convention. In C++ language modes, the
functions cannot be a nonstatic member function, or a consteval
function.

Additionally, these reuse the same priority logic as the init_priority
attribute which explicitly reserved priorty values <= 100 or > 65535.
So we now diagnose use of reserved priorities the same as we do for the
init_priority attribute, but we downgrade the error to be a warning
which defaults to an error to ease use for implementers like
compiler-rt or libc.
---
 clang/docs/ReleaseNotes.rst   |   5 +
 clang/include/clang/Basic/AttrDocs.td |  10 +-
 clang/include/clang/Basic/DiagnosticGroups.td |   4 +
 .../clang/Basic/DiagnosticSemaKinds.td|  12 ++
 clang/lib/Sema/SemaDeclAttr.cpp   | 142 +++---
 .../PowerPC/aix-destructor-attribute.c|  31 +---
 .../CodeGenCXX/aix-destructor-attribute.cpp   |  31 +---
 .../Sema/constructor-attribute-diag-group.c   |  10 ++
 clang/test/Sema/constructor-attribute.c   |  91 +--
 compiler-rt/CMakeLists.txt|   1 +
 compiler-rt/cmake/config-ix.cmake |   2 +-
 11 files changed, 250 insertions(+), 89 deletions(-)
 create mode 100644 clang/test/Sema/constructor-attribute-diag-group.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f314c9c72fa28b7..b6bfc4cb4093406 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -171,6 +171,11 @@ Attribute Changes in Clang
   automatic diagnostic to use parameters of types that the format style
   supports but that are never the result of default argument promotion, such as
   ``float``. (`#59824: `_)
+- The ``constructor`` and ``destructor`` attributes now diagnose when:
+  - the priority is not between 101 and 65535, inclusive,
+  - the function it is applied to accepts arguments or has a non-void return
+type, or
+  - the function it is applied to is a non-static member function (C++).
 
 Improvements to Clang's diagnostics
 ---
diff --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index 2f9d4d1b7907b20..6d82b8f6aa55d43 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -7251,8 +7251,14 @@ after returning from ``main()`` or when the ``exit()`` 
function has been
 called. Note, ``quick_exit()``, ``_Exit()``, and ``abort()`` prevent a function
 marked ``destructor`` from being called.
 
-The constructor or destructor function should not accept any arguments and its
-return type should be ``void``.
+In general, the constructor or destructor function must use the C calling
+convention, cannot accept any arguments, and its return type should be
+``void``, ``int``, or ``unsigned int``. The latter two types are supported for
+historical reasons. On targets with a GNU environment (one which uses glibc),
+the signature of the function can also be the same as that of ``main()``.
+
+In C++ language modes, the function cannot be marked ``consteval``, nor can it
+be a non-static member function.
 
 The attributes accept an optional argument used to specify the priority order
 in which to execute constructor and destructor functions. The priority is
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 0b09c002191848a..e017ca45aeeeb67 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -105,6 +105,10 @@ def EnumConversion : DiagGroup<"enum-conversion",
[EnumEnumConversion,
 EnumFloatConversion,
 EnumCompareConditional]>;
+def InvalidPriority : DiagGroup<"priority-ctor-dtor">;
+// For compatibility with GCC.
+def : DiagGroup<"prio-ctor-dtor", [InvalidPriority]>;
+
 def ObjCSignedCharBoolImplicitIntConversion :
   DiagGroup<"objc-signed-char-bool-implicit-int-conversion">;
 def ImplicitIntConversion : DiagGroup<"implicit-int-conversion",
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index f4eb02fd9570c2f..81ebd3948706bd2 100644
--- a/clang/include/clang/Basic/Diagnosti

[clang] [Clang][Frontend] Fix a crash when -Wdocumentation is used (PR #68525)

2023-10-09 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> > Is there a way we could come up with a test for this?
> 
> Unfortunately, I don't think so. I cannot reduce the 600k lines of 
> preprocessed code to a small test case that will crash the clang frontend.

Have you tried using creduce or other such tool? (We generally don't accept 
patches [without test 
coverage](https://llvm.org/docs/DeveloperPolicy.html#test-cases) unless it 
really isn't possible to test the changes, that doesn't appear to be the 
situation here though.)

https://github.com/llvm/llvm-project/pull/68525
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155688: [PATCH] [llvm] [InstCombine] Canonicalise ADD+GEP

2023-10-09 Thread Nikita Popov via Phabricator via cfe-commits
nikic added a comment.

In D155688#4653347 , @fiigii wrote:

> How does this patch work with `visitGEPOfGEP` that does a reverse 
> transformation?
>
>   // Replace: gep (gep %P, long B), long A, ...
>   // With:T = long A+B; gep %P, T, ...

The reverse transform is only done if `A + B` simplifies.



By the way, this change did cause some code size regressions: 
http://llvm-compile-time-tracker.com/compare.php?from=a16f6462d756804276d4b39267b3c19bcd6949fe&to=e13bed4c5f3544c076ce57e36d9a11eefa5a7815&stat=size-text

The one that stood out to me is that btGjkEpa2.cpp from bullet has become 13% 
larger.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155688/new/

https://reviews.llvm.org/D155688

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 85feb93 - [OpenMP] Fix setting visibility on declare target variables

2023-10-09 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2023-10-09T07:56:43-05:00
New Revision: 85feb9347f77859a877e767692e1c11d00cf6ffd

URL: 
https://github.com/llvm/llvm-project/commit/85feb9347f77859a877e767692e1c11d00cf6ffd
DIFF: 
https://github.com/llvm/llvm-project/commit/85feb9347f77859a877e767692e1c11d00cf6ffd.diff

LOG: [OpenMP] Fix setting visibility on declare target variables

Summary:
A previous patch changed the logic to force external visibliity on
declare target variables. This is because they need to be exported in
the dynamic symbol table to be usable as the standard depicts. However,
the logic was always setting the visibility to `protected`, which would
override some symbols. For example, when calling `libc` functions for
CPU offloading. This patch changes the logic to only fire if the
variable has hidden visibliity to start with.

Added: 


Modified: 
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/OpenMP/declare_target_codegen.cpp
clang/test/OpenMP/declare_target_constexpr_codegen.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index cae9dd93bc55921..d6ab7b3567b9b03 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1391,6 +1391,10 @@ void 
CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
   if (!D)
 return;
 
+  // Set visibility for definitions, and for declarations if requested globally
+  // or set explicitly.
+  LinkageInfo LV = D->getLinkageAndVisibility();
+
   // OpenMP declare target variables must be visible to the host so they can
   // be registered. We require protected visibility unless the variable has
   // the DT_nohost modifier and does not need to be registered.
@@ -1398,14 +1402,12 @@ void 
CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
   Context.getLangOpts().OpenMPIsTargetDevice && isa(D) &&
   D->hasAttr() &&
   D->getAttr()->getDevType() !=
-  OMPDeclareTargetDeclAttr::DT_NoHost) {
+  OMPDeclareTargetDeclAttr::DT_NoHost &&
+  LV.getVisibility() == HiddenVisibility) {
 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
 return;
   }
 
-  // Set visibility for definitions, and for declarations if requested globally
-  // or set explicitly.
-  LinkageInfo LV = D->getLinkageAndVisibility();
   if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
 // Reject incompatible dlllstorage and visibility annotations.
 if (!LV.isVisibilityExplicit())

diff  --git a/clang/test/OpenMP/declare_target_codegen.cpp 
b/clang/test/OpenMP/declare_target_codegen.cpp
index 225695feae95151..71c742198af6bff 100644
--- a/clang/test/OpenMP/declare_target_codegen.cpp
+++ b/clang/test/OpenMP/declare_target_codegen.cpp
@@ -31,7 +31,7 @@
 // CHECK-DAG: @dy = {{protected | }}global i32 0,
 // CHECK-DAG: @bbb = {{protected | }}global i32 0,
 // CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb,
-// CHECK-DAG: @ccc = external {{protected | }}global i32,
+// CHECK-DAG: @ccc = external global i32,
 // CHECK-DAG: @ddd = {{protected | }}global i32 0,
 // CHECK-DAG: @hhh_decl_tgt_ref_ptr = weak global ptr null
 // CHECK-DAG: @ggg_decl_tgt_ref_ptr = weak global ptr null

diff  --git a/clang/test/OpenMP/declare_target_constexpr_codegen.cpp 
b/clang/test/OpenMP/declare_target_constexpr_codegen.cpp
index 2b256cd6a4c7f09..0acd98129394b8a 100644
--- a/clang/test/OpenMP/declare_target_constexpr_codegen.cpp
+++ b/clang/test/OpenMP/declare_target_constexpr_codegen.cpp
@@ -16,7 +16,7 @@ class A {
 public:
   static constexpr double pi = 3.141592653589793116;
 //.
-// CHECK: @_ZN1A2piE = linkonce_odr protected constant double 
0x400921FB54442D18, comdat, align 8
+// CHECK: @_ZN1A2piE = linkonce_odr constant double 0x400921FB54442D18, 
comdat, align 8
 // CHECK: @_ZL9anotherPi = internal constant double 3.14e+00, align 8
 // CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr 
@"__ZN1A2piE$ref", ptr @"__ZL9anotherPi$ref"], section "llvm.metadata"
 //.



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   >