[clang] First PR to llvm, adding feature for issue (PR #123543)

2025-01-19 Thread via cfe-commits

github-actions[bot] wrote:

⚠️ We detected that you are using a GitHub private e-mail address to contribute 
to the repo. Please turn off [Keep my email addresses 
private](https://github.com/settings/emails) setting in your account. See 
[LLVM 
Discourse](https://discourse.llvm.org/t/hidden-emails-on-github-should-we-do-something-about-it)
 for more information.

https://github.com/llvm/llvm-project/pull/123543
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] First PR to llvm, adding feature for issue (PR #123543)

2025-01-19 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: None (Shakil582)


Changes

resolves #43831 

I'll test it a bit more tomorrow. I think I saw something weird with the 
--offset functionality when testing, but my additions  worked for the test 
cases I gave it.


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


1 Files Affected:

- (modified) clang/tools/clang-format/ClangFormat.cpp (+84) 


``diff
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index 28610052b9b74a..64af04ef4c007c 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -26,6 +26,12 @@
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/Process.h"
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 using namespace llvm;
 using clang::tooling::Replacements;
@@ -214,6 +220,9 @@ static cl::opt ListIgnored("list-ignored",
  cl::desc("List ignored files."),
  cl::cat(ClangFormatCategory), cl::Hidden);
 
+static SmallVector OnWords;
+static SmallVector OffWords;
+
 namespace clang {
 namespace format {
 
@@ -307,6 +316,72 @@ static bool fillRanges(MemoryBuffer *Code,
 unsigned Length = Sources.getFileOffset(End) - Offset;
 Ranges.push_back(tooling::Range(Offset, Length));
   }
+
+  if (!OnWords.empty() && !OffWords.empty()) {
+StringRef CodeRef = Code->getBuffer();
+std::set OnSet;
+if (Ranges.empty())
+  OnSet.insert(0);
+else
+  OnSet.insert(Ranges[0].getOffset());
+std::set OffSet;
+if (Ranges.size() == 1 && Ranges[0].getOffset() == 0 &&
+Ranges[0].getLength() == CodeRef.size()) {
+  Ranges.pop_back();
+}
+
+for (const auto &Word : OffWords) {
+  size_t TempOffset = CodeRef.find(Word);
+  while (TempOffset != StringRef::npos) {
+if (TempOffset > *OnSet.begin())
+  OffSet.insert(TempOffset);
+TempOffset = CodeRef.find(Word, TempOffset + 1);
+  }
+}
+
+for (const auto &Word : OnWords) {
+  size_t TempOffset = CodeRef.find(Word);
+  while (TempOffset != StringRef::npos) {
+OnSet.insert(TempOffset);
+TempOffset = CodeRef.find(Word, TempOffset + 1);
+  }
+}
+
+while (!OnSet.empty()) {
+  size_t Offset = OnSet.extract(OnSet.begin()).value();
+  size_t Length;
+  if (!OffSet.empty())
+Length = OffSet.extract(OffSet.begin()).value() - Offset;
+  else
+Length = CodeRef.size() - Offset;
+
+  // Could result in loss of data
+  tooling::Range NewRange = {static_cast(Offset),
+ static_cast(Length)};
+
+  Ranges.push_back(NewRange);
+}
+  }
+
+  std::sort(Ranges.begin(), Ranges.end(),
+[](const tooling::Range &a, const tooling::Range &b) {
+  return a.getOffset() + a.getLength() <
+ b.getOffset() + b.getLength();
+});
+
+  auto start = Ranges.begin();
+  for (auto i = Ranges.begin() + 1; i != Ranges.end(); ++i) {
+if (start->getOffset() + start->getLength() >= i->getOffset()) {
+  tooling::Range Temp(std::min(start->getOffset(), i->getOffset()),
+  std::max(start->getLength(), i->getLength()));
+  Ranges.erase(start, i + 1);
+  start = Ranges.insert(start, Temp);
+  i = start;
+} else {
+  ++start;
+}
+  }
+
   return false;
 }
 
@@ -646,6 +721,15 @@ static bool isIgnored(StringRef FilePath) {
 
   const auto Pathname{convert_to_slash(AbsPath)};
   for (const auto &Pat : Patterns) {
+
+if (Pat.slice(0, 3).equals_insensitive("on:")) {
+  OnWords.push_back(Pat.slice(3, Pat.size()).trim().str());
+  continue;
+}
+if (Pat.slice(0, 4).equals_insensitive("off:")) {
+  OffWords.push_back(Pat.slice(4, Pat.size()).trim().str());
+  continue;
+}
 const bool IsNegated = Pat[0] == '!';
 StringRef Pattern{Pat};
 if (IsNegated)

``




https://github.com/llvm/llvm-project/pull/123543
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] First PR to llvm, adding feature for issue (PR #123543)

2025-01-19 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write 
permissions for the repository. In which case you can instead tag reviewers by 
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a 
review by "ping"ing the PR by adding a comment “Ping”. The common courtesy 
"ping" rate is once a week. Please remember that you are asking for valuable 
time from other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/123543
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] First PR to llvm, adding feature for issue (PR #123543)

2025-01-19 Thread via cfe-commits

https://github.com/Shakil582 created 
https://github.com/llvm/llvm-project/pull/123543

resolves #43831 

I'll test it a bit more tomorrow. I think I saw something weird with the 
--offset functionality when testing, but my additions  worked for the test 
cases I gave it.


>From 4dc0383b366b3105aa879732332062eb2902b213 Mon Sep 17 00:00:00 2001
From: Shakil Usman 
Date: Sun, 19 Jan 2025 21:41:50 -0500
Subject: [PATCH 1/2] 43831 added off on feature to clang

---
 clang/tools/clang-format/ClangFormat.cpp | 155 +++
 1 file changed, 155 insertions(+)

diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index 28610052b9b74a..e5c70979483e24 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -26,6 +26,12 @@
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/Process.h"
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 using namespace llvm;
 using clang::tooling::Replacements;
@@ -214,6 +220,9 @@ static cl::opt ListIgnored("list-ignored",
  cl::desc("List ignored files."),
  cl::cat(ClangFormatCategory), cl::Hidden);
 
+static SmallVector OnWords;
+static SmallVector OffWords;
+
 namespace clang {
 namespace format {
 
@@ -277,6 +286,8 @@ static bool fillRanges(MemoryBuffer *Code,
 return false;
   }
 
+  
+
   if (Offsets.empty())
 Offsets.push_back(0);
   if (Offsets.size() != Lengths.size() &&
@@ -307,6 +318,139 @@ static bool fillRanges(MemoryBuffer *Code,
 unsigned Length = Sources.getFileOffset(End) - Offset;
 Ranges.push_back(tooling::Range(Offset, Length));
   }
+
+  std::cout << "Printing ranges " << "\n";
+  for (const auto& i : Ranges)
+  {
+std::cout << i.getOffset() << " " << i.getOffset() + i.getLength() << "\n";
+  }
+
+  if (!OnWords.empty() && !OffWords.empty())
+  {
+StringRef CodeRef = Code->getBuffer();
+std::set OnSet;
+if (Ranges.empty()) 
+{
+OnSet.insert(0);
+} else {
+OnSet.insert(Ranges[0].getOffset());
+}
+std::set OffSet;
+std::cout << "OFF" << "\n";
+if (Ranges.size() == 1 && Ranges[0].getOffset() == 0 && 
Ranges[0].getLength() == CodeRef.size())
+{
+  Ranges.pop_back();
+}
+for (const auto& Word : OffWords)
+{
+  std::cout << Word << "\n";
+  size_t TempOffset = CodeRef.find(Word);
+  while (TempOffset != StringRef::npos)
+  {
+std::cout << "found offword at " << TempOffset << "\n";
+if (TempOffset > *OnSet.begin())
+{
+OffSet.insert(TempOffset);
+}
+TempOffset = CodeRef.find(Word, TempOffset + 1);
+  }
+}
+
+std::cout << "ON" << "\n";
+for (const auto& Word : OnWords)
+{
+  std::cout << Word << "\n";
+  size_t TempOffset = CodeRef.find(Word);
+  while (TempOffset != StringRef::npos)
+  {
+std::cout << "found onword at " << TempOffset << "\n";
+OnSet.insert(TempOffset);
+TempOffset = CodeRef.find(Word, TempOffset + 1);
+  }
+}
+
+std::cout << "Printing ranges " << "\n";
+for (const auto& i : Ranges)
+{
+std::cout << i.getOffset() << " " << i.getOffset() + i.getLength() << 
"\n";
+}
+
+std::cout << "Entering while" << "\n";
+while (!OnSet.empty())
+{
+  size_t Offset = OnSet.extract(OnSet.begin()).value();
+  size_t Length;
+  if (!OffSet.empty())
+  { 
+Length = OffSet.extract(OffSet.begin()).value() - Offset;
+  } 
+  else
+  {
+Length = CodeRef.size() - Offset;
+  }
+
+  std::cout << Offset << " " << Offset + Length << std::endl;
+  // Could result in loss of data
+  tooling::Range NewRange = {static_cast(Offset), 
static_cast(Length)};
+  
+  /*bool inserted = false;
+  for (auto i = Ranges.begin(); i != Ranges.end(); ++i)
+  {
+if (i->overlapsWith(NewRange))
+{
+  std::cout << "Overlap erasure" << std::endl;
+  tooling::Range Temp(std::min(NewRange.getOffset(), i->getOffset()), 
std::max(NewRange.getLength(), i->getLength()));
+  std::cout << "Old range" << "\n";
+  std::cout << i->getOffset() << " " << i->getOffset() + 
i->getLength() << "\n";
+  std::cout << "New range before insert " << "\n";
+  std::cout << NewRange.getOffset() << " " << NewRange.getOffset() + 
NewRange.getLength() << "\n";
+  Ranges.erase(i);
+  Ranges.insert(i, Temp);
+  inserted = true;
+  std::cout << "After" << "\n";
+  std::cout << Temp.getOffset() << " " << Temp.getOffset() + 
Temp.getLength() << "\n"; 
+}
+  }
+  if (!inserted)
+  {*/
+  Ranges.push_back(NewRange);
+  //}
+}
+  }
+
+  std::sort(Ranges.begin(), Ranges.end(), [](const tooling::Range& a, const 
tooling::Range& b) {
+return a.getOffset() + a.getLengt