[clang-tools-extra] Fix is spelled in source bug (PR #76668)

2023-12-31 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clangd

Author: None (schenka0)


Changes

This fixes the issue reported in #76667 and adds an initial unit test 
for isSpelledInSource().

Note that in that issue there was still some underlying corrupted AST, but this 
at least makes isSpelledInSource() robust to it.

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


2 Files Affected:

- (modified) clang-tools-extra/clangd/SourceCode.cpp (+6-1) 
- (modified) clang-tools-extra/clangd/unittests/SourceCodeTests.cpp (+15) 


``diff
diff --git a/clang-tools-extra/clangd/SourceCode.cpp 
b/clang-tools-extra/clangd/SourceCode.cpp
index 835038423fdf37..8c573cc6fc064a 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -232,7 +232,12 @@ bool isSpelledInSource(SourceLocation Loc, const 
SourceManager ) {
   if (Loc.isFileID())
 return true;
   auto Spelling = SM.getDecomposedSpellingLoc(Loc);
-  StringRef SpellingFile = SM.getSLocEntry(Spelling.first).getFile().getName();
+  bool InvalidSLocEntry = false;
+  const auto SLocEntry = SM.getSLocEntry(Spelling.first, );
+  if (InvalidSLocEntry) {
+return false;
+  }
+  const StringRef SpellingFile = SLocEntry.getFile().getName();
   if (SpellingFile == "")
 return false;
   if (SpellingFile == "")
diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp 
b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
index 08abde87df6d4d..5dced4d317c605 100644
--- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -813,6 +813,21 @@ TEST(SourceCodeTests, isKeywords) {
   EXPECT_FALSE(isKeyword("override", LangOpts));
 }
 
+TEST(SourceCodeTests, isSpelledInSource) {
+  Annotations Test(R"cpp(
+int abc = 1;
+)cpp");
+
+  ParsedAST AST = TestTU::withCode(Test.code()).build();
+  llvm::errs() << Test.code();
+  const SourceManager  = AST.getSourceManager();
+
+  EXPECT_TRUE(
+  isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), SM));
+  EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM));
+  EXPECT_FALSE(isSpelledInSource(SourceLocation::getFromRawEncoding(-1), SM));
+}
+
 struct IncrementalTestStep {
   llvm::StringRef Src;
   llvm::StringRef Contents;

``




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


[clang-tools-extra] Fix is spelled in source bug (PR #76668)

2023-12-31 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/76668
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Fix is spelled in source bug (PR #76668)

2023-12-31 Thread via cfe-commits

https://github.com/schenka0 created 
https://github.com/llvm/llvm-project/pull/76668

This fixes the issue reported in #76667 and adds an initial unit test for 
isSpelledInSource().

Note that in that issue there was still some underlying corrupted AST, but this 
at least makes isSpelledInSource() robust to it.

>From c9e2b9ad57aa9bac52324c91fe6d4ec1aa39ff41 Mon Sep 17 00:00:00 2001
From: schenka0 <154034018+schen...@users.noreply.github.com>
Date: Mon, 1 Jan 2024 01:31:05 -0500
Subject: [PATCH 1/2] Check for invalid SLocEntry before getting spelling

---
 clang-tools-extra/clangd/SourceCode.cpp|  7 ++-
 .../clangd/unittests/SourceCodeTests.cpp   | 14 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/SourceCode.cpp 
b/clang-tools-extra/clangd/SourceCode.cpp
index 835038423fdf37..64e0acb322350c 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -232,7 +232,12 @@ bool isSpelledInSource(SourceLocation Loc, const 
SourceManager ) {
   if (Loc.isFileID())
 return true;
   auto Spelling = SM.getDecomposedSpellingLoc(Loc);
-  StringRef SpellingFile = SM.getSLocEntry(Spelling.first).getFile().getName();
+  bool InvalidSLocEntry = false;
+  const auto SLocEntry = SM.getSLocEntry(Spelling.first, );
+  if(InvalidSLocEntry) {
+return false;
+  }
+  const StringRef SpellingFile = SLocEntry.getFile().getName();
   if (SpellingFile == "")
 return false;
   if (SpellingFile == "")
diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp 
b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
index 08abde87df6d4d..be052dd265e81f 100644
--- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -813,6 +813,20 @@ TEST(SourceCodeTests, isKeywords) {
   EXPECT_FALSE(isKeyword("override", LangOpts));
 }
 
+TEST(SourceCodeTests, isSpelledInSource) {
+Annotations Test(R"cpp(
+int abc = 1;
+)cpp");
+
+  ParsedAST AST = TestTU::withCode(Test.code()).build();
+  llvm::errs() << Test.code();
+  const SourceManager  = AST.getSourceManager();
+
+  EXPECT_TRUE(isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), 
SM));
+  EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM));
+  EXPECT_FALSE(isSpelledInSource(SourceLocation::getFromRawEncoding(-1), SM));
+}
+
 struct IncrementalTestStep {
   llvm::StringRef Src;
   llvm::StringRef Contents;

>From 833e1dbb45a58e8c8b9d6a9087a92df19250e378 Mon Sep 17 00:00:00 2001
From: schenka0 <154034018+schen...@users.noreply.github.com>
Date: Mon, 1 Jan 2024 01:37:29 -0500
Subject: [PATCH 2/2] clang format changes

---
 clang-tools-extra/clangd/SourceCode.cpp| 2 +-
 clang-tools-extra/clangd/unittests/SourceCodeTests.cpp | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clangd/SourceCode.cpp 
b/clang-tools-extra/clangd/SourceCode.cpp
index 64e0acb322350c..8c573cc6fc064a 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -234,7 +234,7 @@ bool isSpelledInSource(SourceLocation Loc, const 
SourceManager ) {
   auto Spelling = SM.getDecomposedSpellingLoc(Loc);
   bool InvalidSLocEntry = false;
   const auto SLocEntry = SM.getSLocEntry(Spelling.first, );
-  if(InvalidSLocEntry) {
+  if (InvalidSLocEntry) {
 return false;
   }
   const StringRef SpellingFile = SLocEntry.getFile().getName();
diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp 
b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
index be052dd265e81f..5dced4d317c605 100644
--- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -814,7 +814,7 @@ TEST(SourceCodeTests, isKeywords) {
 }
 
 TEST(SourceCodeTests, isSpelledInSource) {
-Annotations Test(R"cpp(
+  Annotations Test(R"cpp(
 int abc = 1;
 )cpp");
 
@@ -822,7 +822,8 @@ TEST(SourceCodeTests, isSpelledInSource) {
   llvm::errs() << Test.code();
   const SourceManager  = AST.getSourceManager();
 
-  EXPECT_TRUE(isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), 
SM));
+  EXPECT_TRUE(
+  isSpelledInSource(SM.getLocForStartOfFile(SM.getMainFileID()), SM));
   EXPECT_TRUE(isSpelledInSource(SourceLocation(), SM));
   EXPECT_FALSE(isSpelledInSource(SourceLocation::getFromRawEncoding(-1), SM));
 }

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