[clang-tools-extra] [clang-tidy] Add AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-31 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL closed 
https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-31 Thread Congcong Cai via cfe-commits

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


https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-31 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL updated 
https://github.com/llvm/llvm-project/pull/71701

>From 050d53bdbc01be93a15f6851faddc24e520f0472 Mon Sep 17 00:00:00 2001
From: Piotr Zegar 
Date: Wed, 8 Nov 2023 16:52:06 +
Subject: [PATCH 1/4] [clang-tidy] Add AllowStringArrays option to
 modernize-avoid-c-arrays

Add AllowStringArrays option, enabling the exclusion of array types with deduced
sizes constructed from string literals. This includes only var declarations
of array of characters constructed directly from c-strings.
---
 .../modernize/AvoidCArraysCheck.cpp   | 21 +--
 .../clang-tidy/modernize/AvoidCArraysCheck.h  | 14 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 +
 .../checks/modernize/avoid-c-arrays.rst   |  9 
 .../avoid-c-arrays-ignores-strings.cpp|  6 ++
 .../checkers/modernize/avoid-c-arrays.cpp |  3 +++
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
index d1b15479ffe7a..ab1cdd62aa2cc 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -12,6 +12,8 @@
 
 using namespace clang::ast_matchers;
 
+namespace clang::tidy::modernize {
+
 namespace {
 
 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
@@ -38,16 +40,31 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
 
 } // namespace
 
-namespace clang::tidy::modernize {
+AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowStringArrays(Options.get("AllowStringArrays", false)) {}
+
+void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "AllowStringArrays", AllowStringArrays);
+}
 
 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
+  ast_matchers::internal::Matcher IgnoreStringArrayIfNeededMatcher =
+  anything();
+  if (AllowStringArrays)
+IgnoreStringArrayIfNeededMatcher =
+unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
+   hasElementType(isAnyCharacter(),
+   hasParent(varDecl(hasInitializer(stringLiteral());
+
   Finder->addMatcher(
   typeLoc(hasValidBeginLoc(), hasType(arrayType()),
   unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext(),
-   hasAncestor(functionDecl(isExternC())
+   hasAncestor(functionDecl(isExternC(),
+  std::move(IgnoreStringArrayIfNeededMatcher))
   .bind("typeloc"),
   this);
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
index 7099f99c86949..719e88e4b3166 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
@@ -19,13 +19,19 @@ namespace clang::tidy::modernize {
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-c-arrays.html
 class AvoidCArraysCheck : public ClangTidyCheck {
 public:
-  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
-  : ClangTidyCheck(Name, Context) {}
+  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
   bool isLanguageVersionSupported(const LangOptions ) const override {
 return LangOpts.CPlusPlus11;
   }
-  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
-  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  std::optional getCheckTraversalKind() const override {
+return TK_IgnoreUnlessSpelledInSource;
+  }
+
+private:
+  const bool AllowStringArrays;
 };
 
 } // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fd2cba4e4f463..94b51f919f80a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -106,6 +106,11 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`modernize-avoid-c-arrays
+  ` check by introducing the new
+  `AllowStringArrays` option, enabling the exclusion of array types with 
deduced
+  sizes constructed from string literals.
+
 Removed checks
 ^^
 
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/modernize/avoid-c-arrays.rst 

[clang-tools-extra] [clang-tidy] Add AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-29 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> It should not work, as this is not array, but pointer

Agree. But it fulfills the description of this opinion, since it looks like to 
construct a array from string literals.
It would be better to explain it in document.

> incomplete array types constructed from string literals will be ignored.

https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-29 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 3e47e75febc8fefa19afe1e8ef2b15a106d2f791 
b4c4573d505fe0df63ed4252fb49e46e5c1a7afb -- 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp
 clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
index 8118d83358..89790ea70c 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -53,8 +53,10 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder 
*Finder) {
   anything();
   if (AllowStringArrays)
 IgnoreStringArrayIfNeededMatcher =
-
unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(hasElementType(isAnyCharacter(),
-   hasParent(varDecl(hasInitializer(stringLiteral()), 
unless(parmVarDecl());
+unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
+   hasElementType(isAnyCharacter(),
+   hasParent(varDecl(hasInitializer(stringLiteral()),
+ unless(parmVarDecl());
 
   Finder->addMatcher(
   typeLoc(hasValidBeginLoc(), hasType(arrayType()),

``




https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-29 Thread Piotr Zegar via cfe-commits

PiotrZSL wrote:

> I may not work for
> 
> ```c++
> void f(const char name[]);
> 
> f("111");
> ```

It should not work, as this is not array, but pointer. If you do `sizeof(name)` 
you get size of pointer, instead of number of elements in array.

https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-29 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL updated 
https://github.com/llvm/llvm-project/pull/71701

>From 050d53bdbc01be93a15f6851faddc24e520f0472 Mon Sep 17 00:00:00 2001
From: Piotr Zegar 
Date: Wed, 8 Nov 2023 16:52:06 +
Subject: [PATCH 1/2] [clang-tidy] Add AllowStringArrays option to
 modernize-avoid-c-arrays

Add AllowStringArrays option, enabling the exclusion of array types with deduced
sizes constructed from string literals. This includes only var declarations
of array of characters constructed directly from c-strings.
---
 .../modernize/AvoidCArraysCheck.cpp   | 21 +--
 .../clang-tidy/modernize/AvoidCArraysCheck.h  | 14 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 +
 .../checks/modernize/avoid-c-arrays.rst   |  9 
 .../avoid-c-arrays-ignores-strings.cpp|  6 ++
 .../checkers/modernize/avoid-c-arrays.cpp |  3 +++
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
index d1b15479ffe7a..ab1cdd62aa2cc 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -12,6 +12,8 @@
 
 using namespace clang::ast_matchers;
 
+namespace clang::tidy::modernize {
+
 namespace {
 
 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
@@ -38,16 +40,31 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
 
 } // namespace
 
-namespace clang::tidy::modernize {
+AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowStringArrays(Options.get("AllowStringArrays", false)) {}
+
+void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "AllowStringArrays", AllowStringArrays);
+}
 
 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
+  ast_matchers::internal::Matcher IgnoreStringArrayIfNeededMatcher =
+  anything();
+  if (AllowStringArrays)
+IgnoreStringArrayIfNeededMatcher =
+unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
+   hasElementType(isAnyCharacter(),
+   hasParent(varDecl(hasInitializer(stringLiteral());
+
   Finder->addMatcher(
   typeLoc(hasValidBeginLoc(), hasType(arrayType()),
   unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext(),
-   hasAncestor(functionDecl(isExternC())
+   hasAncestor(functionDecl(isExternC(),
+  std::move(IgnoreStringArrayIfNeededMatcher))
   .bind("typeloc"),
   this);
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
index 7099f99c86949..719e88e4b3166 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
@@ -19,13 +19,19 @@ namespace clang::tidy::modernize {
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-c-arrays.html
 class AvoidCArraysCheck : public ClangTidyCheck {
 public:
-  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
-  : ClangTidyCheck(Name, Context) {}
+  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
   bool isLanguageVersionSupported(const LangOptions ) const override {
 return LangOpts.CPlusPlus11;
   }
-  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
-  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  std::optional getCheckTraversalKind() const override {
+return TK_IgnoreUnlessSpelledInSource;
+  }
+
+private:
+  const bool AllowStringArrays;
 };
 
 } // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fd2cba4e4f463..94b51f919f80a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -106,6 +106,11 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`modernize-avoid-c-arrays
+  ` check by introducing the new
+  `AllowStringArrays` option, enabling the exclusion of array types with 
deduced
+  sizes constructed from string literals.
+
 Removed checks
 ^^
 
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/modernize/avoid-c-arrays.rst 

[clang-tools-extra] [clang-tidy] Add AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-25 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 commented:

I may not work for 
```c++
void f(const char name[]);

f("111");
```

https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-21 Thread Julian Schmidt via cfe-commits

https://github.com/5chmidti approved this pull request.

LGTM, but I'd prefer a second person to approve as well.

https://github.com/llvm/llvm-project/pull/71701
___
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 AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2023-11-08 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)


Changes

Add AllowStringArrays option, enabling the exclusion of array types with 
deduced sizes constructed from string literals. This includes only var 
declarations of array of characters constructed directly from c-strings.

Closes #59475

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


6 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
(+19-2) 
- (modified) clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h (+10-4) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5) 
- (modified) 
clang-tools-extra/docs/clang-tidy/checks/modernize/avoid-c-arrays.rst (+9) 
- (added) 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp
 (+6) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp (+3) 


``diff
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
index d1b15479ffe7a93..ab1cdd62aa2cc01 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -12,6 +12,8 @@
 
 using namespace clang::ast_matchers;
 
+namespace clang::tidy::modernize {
+
 namespace {
 
 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
@@ -38,16 +40,31 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
 
 } // namespace
 
-namespace clang::tidy::modernize {
+AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowStringArrays(Options.get("AllowStringArrays", false)) {}
+
+void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "AllowStringArrays", AllowStringArrays);
+}
 
 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
+  ast_matchers::internal::Matcher IgnoreStringArrayIfNeededMatcher =
+  anything();
+  if (AllowStringArrays)
+IgnoreStringArrayIfNeededMatcher =
+unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
+   hasElementType(isAnyCharacter(),
+   hasParent(varDecl(hasInitializer(stringLiteral());
+
   Finder->addMatcher(
   typeLoc(hasValidBeginLoc(), hasType(arrayType()),
   unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext(),
-   hasAncestor(functionDecl(isExternC())
+   hasAncestor(functionDecl(isExternC(),
+  std::move(IgnoreStringArrayIfNeededMatcher))
   .bind("typeloc"),
   this);
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
index 7099f99c869498f..719e88e4b31662f 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
@@ -19,13 +19,19 @@ namespace clang::tidy::modernize {
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-c-arrays.html
 class AvoidCArraysCheck : public ClangTidyCheck {
 public:
-  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
-  : ClangTidyCheck(Name, Context) {}
+  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
   bool isLanguageVersionSupported(const LangOptions ) const override {
 return LangOpts.CPlusPlus11;
   }
-  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
-  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  std::optional getCheckTraversalKind() const override {
+return TK_IgnoreUnlessSpelledInSource;
+  }
+
+private:
+  const bool AllowStringArrays;
 };
 
 } // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fe8c7175d554c7b..97e532abbf8c715 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -330,6 +330,11 @@ Changes in existing checks
   ` check to
   not emit a ``return`` for fixes when the function returns ``void``.
 
+- Improved :doc:`modernize-avoid-c-arrays
+  ` check by introducing the new
+  `AllowStringArrays` option, enabling the exclusion of array types with 
deduced
+  sizes constructed from string literals.
+
 - Improved :doc:`modernize-loop-convert
   ` to support for-loops with
   iterators initialized by free functions like ``begin``, ``end``, or ``size``
diff --git 

[clang-tools-extra] [clang-tidy] Add AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2023-11-08 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL created 
https://github.com/llvm/llvm-project/pull/71701

Add AllowStringArrays option, enabling the exclusion of array types with 
deduced sizes constructed from string literals. This includes only var 
declarations of array of characters constructed directly from c-strings.

Closes #59475

>From 8301beef04a70e54232cafdb24949e8f961b2aa7 Mon Sep 17 00:00:00 2001
From: Piotr Zegar 
Date: Wed, 8 Nov 2023 16:52:06 +
Subject: [PATCH] [clang-tidy] Add AllowStringArrays option to
 modernize-avoid-c-arrays

Add AllowStringArrays option, enabling the exclusion of array types with deduced
sizes constructed from string literals. This includes only var declarations
of array of characters constructed directly from c-strings.
---
 .../modernize/AvoidCArraysCheck.cpp   | 21 +--
 .../clang-tidy/modernize/AvoidCArraysCheck.h  | 14 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 +
 .../checks/modernize/avoid-c-arrays.rst   |  9 
 .../avoid-c-arrays-ignores-strings.cpp|  6 ++
 .../checkers/modernize/avoid-c-arrays.cpp |  3 +++
 6 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays-ignores-strings.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
index d1b15479ffe7a93..ab1cdd62aa2cc01 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp
@@ -12,6 +12,8 @@
 
 using namespace clang::ast_matchers;
 
+namespace clang::tidy::modernize {
+
 namespace {
 
 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
@@ -38,16 +40,31 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
 
 } // namespace
 
-namespace clang::tidy::modernize {
+AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowStringArrays(Options.get("AllowStringArrays", false)) {}
+
+void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap ) {
+  Options.store(Opts, "AllowStringArrays", AllowStringArrays);
+}
 
 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
+  ast_matchers::internal::Matcher IgnoreStringArrayIfNeededMatcher =
+  anything();
+  if (AllowStringArrays)
+IgnoreStringArrayIfNeededMatcher =
+unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
+   hasElementType(isAnyCharacter(),
+   hasParent(varDecl(hasInitializer(stringLiteral());
+
   Finder->addMatcher(
   typeLoc(hasValidBeginLoc(), hasType(arrayType()),
   unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext(),
-   hasAncestor(functionDecl(isExternC())
+   hasAncestor(functionDecl(isExternC(),
+  std::move(IgnoreStringArrayIfNeededMatcher))
   .bind("typeloc"),
   this);
 }
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h 
b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
index 7099f99c869498f..719e88e4b31662f 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.h
@@ -19,13 +19,19 @@ namespace clang::tidy::modernize {
 /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/avoid-c-arrays.html
 class AvoidCArraysCheck : public ClangTidyCheck {
 public:
-  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
-  : ClangTidyCheck(Name, Context) {}
+  AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  void storeOptions(ClangTidyOptions::OptionMap ) override;
   bool isLanguageVersionSupported(const LangOptions ) const override {
 return LangOpts.CPlusPlus11;
   }
-  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
-  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+  std::optional getCheckTraversalKind() const override {
+return TK_IgnoreUnlessSpelledInSource;
+  }
+
+private:
+  const bool AllowStringArrays;
 };
 
 } // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fe8c7175d554c7b..97e532abbf8c715 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -330,6 +330,11 @@ Changes in existing checks
   ` check to
   not emit a ``return`` for fixes when the function returns ``void``.
 
+- Improved :doc:`modernize-avoid-c-arrays
+  ` check by