[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-16 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83508#2143625 , @sammccall wrote:

> Your test cases show two problems with this strategy:
>
> - we ignore comments and semicolons, but not preprocessor directives (or 
> disabled preprocessor regions). I think we can fix this by asking TokenBuffer 
> if a spelled token is part of a region that maps to no (PP-)expanded tokens.


I have tried this locally. seems it breaks SelectionTest.IncludedFile test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508



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


[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-16 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83508#2155322 , @sammccall wrote:

> In D83508#2155174 , @ArcsinX wrote:
>
> > In D83508#2143625 , @sammccall 
> > wrote:
> >
> > > Your test cases show two problems with this strategy:
> > >
> > > - we ignore comments and semicolons, but not preprocessor directives (or 
> > > disabled preprocessor regions). I think we can fix this by asking 
> > > TokenBuffer if a spelled token is part of a region that maps to no 
> > > (PP-)expanded tokens.
> >
> >
> > I have tried this locally. seems it breaks SelectionTest.IncludedFile test.
>
>
> Yeah that makes sense, I guess it just says nothing is selected in that case?


Yes, and test crashes at `T.commonAncestor()` (which is `nullptr`) dereference. 
So can we also add `ASSERT_TRUE(T.commonAncestor());` into several tests?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508



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


[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-16 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 278440.
ArcsinX added a comment.

Prevent tokens selection in disabled preprocessor sections.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508

Files:
  clang-tools-extra/clangd/Selection.cpp
  clang-tools-extra/clangd/unittests/SelectionTests.cpp


Index: clang-tools-extra/clangd/unittests/SelectionTests.cpp
===
--- clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -521,6 +521,7 @@
   EXPECT_THAT(AST.getDiagnostics(), ::testing::IsEmpty());
   auto T = makeSelectionTree(Case, AST);
 
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("BreakStmt", T.commonAncestor()->kind());
   EXPECT_EQ("WhileStmt", T.commonAncestor()->Parent->kind());
 }
@@ -538,7 +539,7 @@
   auto AST = TU.build();
   auto T = makeSelectionTree(Case, AST);
 
-  EXPECT_EQ("WhileStmt", T.commonAncestor()->kind());
+  EXPECT_EQ(T.commonAncestor(), nullptr);
 }
 
 TEST(SelectionTest, MacroArgExpansion) {
@@ -552,6 +553,7 @@
   Annotations Test(Case);
   auto AST = TestTU::withCode(Test.code()).build();
   auto T = makeSelectionTree(Case, AST);
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("IntegerLiteral", T.commonAncestor()->kind());
   EXPECT_TRUE(T.commonAncestor()->Selected);
 
@@ -566,6 +568,7 @@
   AST = TestTU::withCode(Test.code()).build();
   T = makeSelectionTree(Case, AST);
 
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("IntegerLiteral", T.commonAncestor()->kind());
 }
 
@@ -580,6 +583,7 @@
 
   const SelectionTree::Node *Str = T.commonAncestor();
   EXPECT_EQ("StringLiteral", nodeKind(Str)) << "Implicit selected?";
+  ASSERT_NE(Str, nullptr);
   EXPECT_EQ("ImplicitCastExpr", nodeKind(Str->Parent));
   EXPECT_EQ("CXXConstructExpr", nodeKind(Str->Parent->Parent));
   EXPECT_EQ(Str, &Str->Parent->Parent->ignoreImplicit())
@@ -643,6 +647,32 @@
   EXPECT_EQ(1u, Seen) << "one tree for nontrivial selection";
 }
 
+TEST(SelectionTest, DisabledPreprocessor) {
+  const char *Case = R"cpp(
+namespace ns {
+#define FOO B^AR
+}
+  )cpp";
+  Annotations Test(Case);
+  auto TU = TestTU::withCode(Test.code());
+  auto AST = TU.build();
+  auto T = makeSelectionTree(Case, AST);
+  EXPECT_EQ(T.commonAncestor(), nullptr);
+
+  Case = R"cpp(
+namespace ns {
+#if 0
+void fu^nc();
+#endif
+}
+  )cpp";
+  Test = Annotations(Case);
+  TU = TestTU::withCode(Test.code());
+  AST = TU.build();
+  T = makeSelectionTree(Case, AST);
+  EXPECT_EQ(T.commonAncestor(), nullptr);
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/Selection.cpp
===
--- clang-tools-extra/clangd/Selection.cpp
+++ clang-tools-extra/clangd/Selection.cpp
@@ -224,6 +224,11 @@
 for (const syntax::Token *T = SelFirst; T < SelLimit; ++T) {
   if (shouldIgnore(*T))
 continue;
+  // Ignore tokens in disabled preprocessor sections.
+  if (Buf.expandedTokens(SM.getMacroArgExpandedLocation(T->location()))
+  .empty() &&
+  !Buf.expansionStartingAt(T))
+continue;
   SpelledTokens.emplace_back();
   Tok &S = SpelledTokens.back();
   S.Offset = SM.getFileOffset(T->location());


Index: clang-tools-extra/clangd/unittests/SelectionTests.cpp
===
--- clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -521,6 +521,7 @@
   EXPECT_THAT(AST.getDiagnostics(), ::testing::IsEmpty());
   auto T = makeSelectionTree(Case, AST);
 
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("BreakStmt", T.commonAncestor()->kind());
   EXPECT_EQ("WhileStmt", T.commonAncestor()->Parent->kind());
 }
@@ -538,7 +539,7 @@
   auto AST = TU.build();
   auto T = makeSelectionTree(Case, AST);
 
-  EXPECT_EQ("WhileStmt", T.commonAncestor()->kind());
+  EXPECT_EQ(T.commonAncestor(), nullptr);
 }
 
 TEST(SelectionTest, MacroArgExpansion) {
@@ -552,6 +553,7 @@
   Annotations Test(Case);
   auto AST = TestTU::withCode(Test.code()).build();
   auto T = makeSelectionTree(Case, AST);
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("IntegerLiteral", T.commonAncestor()->kind());
   EXPECT_TRUE(T.commonAncestor()->Selected);
 
@@ -566,6 +568,7 @@
   AST = TestTU::withCode(Test.code()).build();
   T = makeSelectionTree(Case, AST);
 
+  ASSERT_NE(T.commonAncestor(), nullptr);
   EXPECT_EQ("IntegerLiteral", T.commonAncestor()->kind());
 }
 
@@ -580,6 +583,7 @@
 
   const SelectionTree::Node *Str = T.commonAncestor();
   EXPECT_EQ("StringLiteral", nodeKind(Str)) << "Implicit selected?";
+  ASSERT_NE(Str, nullptr);
   EXPECT_EQ("ImplicitCastExpr", nodeKind(Str->Parent));
   EXPECT_EQ("CXXConstructExpr", nodeKind(Str->Parent->Par

[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-16 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/Selection.cpp:227
 continue;
+  // Ignore tokens in disabled preprocessor sections.
+  if (Buf.expandedTokens(SM.getMacroArgExpandedLocation(T->location()))

sammccall wrote:
> I think this is more work than we'd like to do in this loop (the whole file 
> could be selected!) and it's also not obviously correct - it relies on the 
> assumption that any token that doesn't expand to something by itself, isn't a 
> macro argument or macro name, is part of an ignored region. (That may or may 
> not be correct, but it's a nontrivial assumption to make here).
> 
> I think the API we need is something like `vector 
> TokenBuffer::expansionsOverlapping(ArrayRef Spelled)`.
> This requires a couple of binary searches on the TokenBuffer side to compute, 
> and on this side we can just look at the spelled token ranges for empty 
> expansions and mark them in a bitmap.
> 
> I'm happy to try to put together a TokenBuffer patch if this is too much of a 
> yak-shave, though.
>  it relies on the assumption that any token that doesn't expand to something 
> by itself, isn't a macro argument or macro name, is part of an ignored 
> region. (That may or may not be correct, but it's a nontrivial assumption to 
> make here).

Example `#define FOO BAR`.
Expanded tokens:
Spelled tokens: `#`, `define`, `FOO`, `BAR`

I think we could ignore all except `FOO` in this case. Also I found similar 
check in XRefs.cpp `bool tokenSpelledAt(SourceLocation SpellingLoc, const 
syntax::TokenBuffer &TB)`

How to judge should we skip token or not if we do not have expanded tokens for 
it? Do you have any advice here?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508



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


[PATCH] D84009: [Syntax] expose API for expansions overlapping a spelled token range.

2020-07-17 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: clang/lib/Tooling/Syntax/Tokens.cpp:252
 return {};
-  assert(Spelled.front().location().isFileID());
-
-  auto FID = sourceManager().getFileID(Spelled.front().location());
-  auto It = Files.find(FID);
-  assert(It != Files.end());
-
-  const MarkedFile &File = It->second;
+  const MarkedFile &File = fileForSpelled(Spelled);
   // `Spelled` must be a subrange of `File.SpelledTokens`.

Could we use `const auto &` here as in lines 419, 434 for consistency?



Comment at: clang/lib/Tooling/Syntax/Tokens.cpp:431
+std::vector
+TokenBuffer::expansionsAffecting(llvm::ArrayRef Spelled) const {
+  if (Spelled.empty())

Will it be useful to have similar API with FileID/MarkedFile parameter?
For example, we already have FileID in `SelectionTester::SelectionTester`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84009



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


[PATCH] D84009: [Syntax] expose API for expansions overlapping a spelled token range.

2020-07-17 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

Thanks. This LGTM, but I think I don't have enough experience to accept 
revisions and could miss something important. So, may be @kadircet could accept 
it if its OK for him.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84009



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


[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-17 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83508#2157859 , @sammccall wrote:

> Tried this out in D84012 /D84009 
> . Works pretty well, and I think the API is 
> a useful and natural addition to TokenBuffer.


For my test cases it works well, so I think this problem is fixed.
Should I abandon this revision?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508



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


[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, kbobyrev, usaxena95, kadircet, arphaman, 
jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

This patch mostly reverts D74850 .
We could not use `AST.getTokens()` here, because it does not have tokens from 
the preamble.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84144

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/SyncAPI.cpp
  clang-tools-extra/clangd/unittests/SyncAPI.h
  clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Index: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
===
--- clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
+++ clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
@@ -523,6 +523,39 @@
WithKind(SymbolKind::Struct), Children();
 }
 
+TEST(TypeHierarchy, Preamble) {
+  Annotations SourceAnnotations(R"cpp(
+#include "header_in_preamble.h"
+struct Ch^ild : Parent {
+  int b;
+};)cpp");
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct Parent {
+  int a;
+};)cpp");
+
+  MockCompilationDatabase CDB;
+  MockFS FS;
+  ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
+
+  // Fill the filesystem.
+  auto FooCpp = testPath("foo.cpp");
+  FS.Files[FooCpp] = "";
+  auto HeaderInPreambleH = testPath("header_in_preamble.h");
+  FS.Files[HeaderInPreambleH] = HeaderInPreambleAnnotations.code().str();
+  runAddDocument(Server, FooCpp, SourceAnnotations.code());
+
+  // Type hierarchy for `Child`
+  auto Result = runTypeHierarchy(Server, FooCpp, SourceAnnotations.point(), 1,
+ TypeHierarchyDirection::Parents);
+  ASSERT_TRUE(bool(Result));
+  ASSERT_TRUE(*Result);
+
+  EXPECT_THAT(**Result, AllOf(WithName("Child"),
+  Parents(AllOf(WithName("Parent"), Parents();
+}
+
 SymbolID findSymbolIDByName(SymbolIndex *Index, llvm::StringRef Name,
 llvm::StringRef TemplateArgs = "") {
   SymbolID Result;
Index: clang-tools-extra/clangd/unittests/SyncAPI.h
===
--- clang-tools-extra/clangd/unittests/SyncAPI.h
+++ clang-tools-extra/clangd/unittests/SyncAPI.h
@@ -60,6 +60,10 @@
 llvm::Expected>
 runSwitchHeaderSource(ClangdServer &Server, PathRef File);
 
+llvm::Expected>
+runTypeHierarchy(ClangdServer &Server, PathRef File, Position Pos, int Resolve,
+ TypeHierarchyDirection Direction);
+
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/unittests/SyncAPI.cpp
===
--- clang-tools-extra/clangd/unittests/SyncAPI.cpp
+++ clang-tools-extra/clangd/unittests/SyncAPI.cpp
@@ -154,5 +154,13 @@
   return std::move(*Result);
 }
 
+llvm::Expected>
+runTypeHierarchy(ClangdServer &Server, PathRef File, Position Pos, int Resolve,
+ TypeHierarchyDirection Direction) {
+  llvm::Optional>> Result;
+  Server.typeHierarchy(File, Pos, Resolve, Direction, capture(Result));
+  return std::move(*Result);
+}
+
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1183,23 +1183,24 @@
 
 // FIXME(nridge): Reduce duplication between this function and declToSym().
 static llvm::Optional
-declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND,
-const syntax::TokenBuffer &TB) {
+declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
   SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
+  SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
+  SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
+  const auto NameRange =
+  toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
+  if (!NameRange)
+return llvm::None;
   auto FilePath =
   getCanonicalPath(SM.getFileEntryForID(SM.getFileID(NameLoc)), SM);
   auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
   if (!FilePath || !TUPath)
 return llvm::None; // Not useful without a uri.
 
-  auto DeclToks = TB.spelledForExpanded(TB.expandedTokens(ND.getSourceRange()));
-  if (!DeclToks || DeclToks->empty())
-return llvm::None;
-
-  auto NameToks = TB.spelledForExpanded(TB.expandedTokens(NameLoc));
-  if (!NameToks || NameToks->empty())
-return llvm::None;
+  Position NameBegin = sourceLocToPosition(SM, NameLoc);
+  Position NameEnd = sourceLocToPosition(
+  SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
 
   index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
   // FIXME: this is not classifying

[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279168.
ArcsinX added a comment.

NameRange => DeclRange
Simplify TypeHierarchy.Preamble test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Index: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
===
--- clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
+++ clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
@@ -523,6 +523,31 @@
WithKind(SymbolKind::Struct), Children();
 }
 
+TEST(TypeHierarchy, Preamble) {
+  Annotations SourceAnnotations(R"cpp(
+#include "header_in_preamble.h"
+struct Ch^ild : Parent {
+  int b;
+};)cpp");
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct Parent {
+  int a;
+};)cpp");
+
+  TestTU TU = TestTU::withCode(SourceAnnotations.code());
+  TU.AdditionalFiles["header_in_preamble.h"] =
+  HeaderInPreambleAnnotations.code().str();
+  auto AST = TU.build();
+
+  llvm::Optional Result = getTypeHierarchy(
+  AST, SourceAnnotations.point(), 1, TypeHierarchyDirection::Parents);
+
+  ASSERT_TRUE(Result);
+  EXPECT_THAT(*Result, AllOf(WithName("Child"),
+ Parents(AllOf(WithName("Parent"), Parents();
+}
+
 SymbolID findSymbolIDByName(SymbolIndex *Index, llvm::StringRef Name,
 llvm::StringRef TemplateArgs = "") {
   SymbolID Result;
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1183,23 +1183,24 @@
 
 // FIXME(nridge): Reduce duplication between this function and declToSym().
 static llvm::Optional
-declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND,
-const syntax::TokenBuffer &TB) {
+declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
   SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
+  SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
+  SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
+  const auto DeclRange =
+  toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
+  if (!DeclRange)
+return llvm::None;
   auto FilePath =
   getCanonicalPath(SM.getFileEntryForID(SM.getFileID(NameLoc)), SM);
   auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
   if (!FilePath || !TUPath)
 return llvm::None; // Not useful without a uri.
 
-  auto DeclToks = TB.spelledForExpanded(TB.expandedTokens(ND.getSourceRange()));
-  if (!DeclToks || DeclToks->empty())
-return llvm::None;
-
-  auto NameToks = TB.spelledForExpanded(TB.expandedTokens(NameLoc));
-  if (!NameToks || NameToks->empty())
-return llvm::None;
+  Position NameBegin = sourceLocToPosition(SM, NameLoc);
+  Position NameEnd = sourceLocToPosition(
+  SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
 
   index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
   // FIXME: this is not classifying constructors, destructors and operators
@@ -1210,12 +1211,9 @@
   THI.name = printName(Ctx, ND);
   THI.kind = SK;
   THI.deprecated = ND.isDeprecated();
-  THI.range = halfOpenToRange(
-  SM, syntax::Token::range(SM, DeclToks->front(), DeclToks->back())
-  .toCharRange(SM));
-  THI.selectionRange = halfOpenToRange(
-  SM, syntax::Token::range(SM, NameToks->front(), NameToks->back())
-  .toCharRange(SM));
+  THI.range = Range{sourceLocToPosition(SM, DeclRange->getBegin()),
+sourceLocToPosition(SM, DeclRange->getEnd())};
+  THI.selectionRange = Range{NameBegin, NameEnd};
   if (!THI.range.contains(THI.selectionRange)) {
 // 'selectionRange' must be contained in 'range', so in cases where clang
 // reports unrelated ranges we need to reconcile somehow.
@@ -1282,8 +1280,7 @@
 
 static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
std::vector &SuperTypes,
-   RecursionProtectionSet &RPSet,
-   const syntax::TokenBuffer &TB) {
+   RecursionProtectionSet &RPSet) {
   // typeParents() will replace dependent template specializations
   // with their class template, so to avoid infinite recursion for
   // certain types of hierarchies, keep the templates encountered
@@ -1298,9 +1295,9 @@
 
   for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
 if (Optional ParentSym =
-declToTypeHierarchyItem(ASTCtx, *ParentDecl, TB)) {
+declToTypeHierarchyItem(ASTCtx, *ParentDecl)) {
   ParentSym->parents.emplace();
-  fillSuperTypes(*ParentDecl

[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked 2 inline comments as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/XRefs.cpp:1191
+  SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
+  const auto NameRange =
+  toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});

kadircet wrote:
> let's rename it to `DeclRange` rather than `NameRange`.
Renamed.



Comment at: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp:550
+  // Type hierarchy for `Child`
+  auto Result = runTypeHierarchy(Server, FooCpp, SourceAnnotations.point(), 1,
+ TypeHierarchyDirection::Parents);

kadircet wrote:
> you can keep using the `TestTU`. just provide the header via 
> `AdditionalFiles` and `TU.build` should first build a preamble and use it. 
> That way you can get rid of additions to the SyncAPI and usage of 
> ClangdServer in here.
Thanks, done.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144



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


[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279174.
ArcsinX added a comment.

Check source range for Parent


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Index: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
===
--- clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
+++ clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
@@ -523,6 +523,35 @@
WithKind(SymbolKind::Struct), Children();
 }
 
+TEST(TypeHierarchy, Preamble) {
+  Annotations SourceAnnotations(R"cpp(
+#include "header_in_preamble.h"
+struct Ch^ild : Parent {
+  int b;
+};)cpp");
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct [[Parent]] {
+  int a;
+};)cpp");
+
+  TestTU TU = TestTU::withCode(SourceAnnotations.code());
+  TU.AdditionalFiles["header_in_preamble.h"] =
+  HeaderInPreambleAnnotations.code().str();
+  auto AST = TU.build();
+
+  llvm::Optional Result = getTypeHierarchy(
+  AST, SourceAnnotations.point(), 1, TypeHierarchyDirection::Parents);
+
+  ASSERT_TRUE(Result);
+  EXPECT_THAT(
+  *Result,
+  AllOf(WithName("Child"),
+Parents(AllOf(WithName("Parent"),
+  SelectionRangeIs(HeaderInPreambleAnnotations.range()),
+  Parents();
+}
+
 SymbolID findSymbolIDByName(SymbolIndex *Index, llvm::StringRef Name,
 llvm::StringRef TemplateArgs = "") {
   SymbolID Result;
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1183,23 +1183,24 @@
 
 // FIXME(nridge): Reduce duplication between this function and declToSym().
 static llvm::Optional
-declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND,
-const syntax::TokenBuffer &TB) {
+declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
   SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
+  SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
+  SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
+  const auto DeclRange =
+  toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
+  if (!DeclRange)
+return llvm::None;
   auto FilePath =
   getCanonicalPath(SM.getFileEntryForID(SM.getFileID(NameLoc)), SM);
   auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
   if (!FilePath || !TUPath)
 return llvm::None; // Not useful without a uri.
 
-  auto DeclToks = TB.spelledForExpanded(TB.expandedTokens(ND.getSourceRange()));
-  if (!DeclToks || DeclToks->empty())
-return llvm::None;
-
-  auto NameToks = TB.spelledForExpanded(TB.expandedTokens(NameLoc));
-  if (!NameToks || NameToks->empty())
-return llvm::None;
+  Position NameBegin = sourceLocToPosition(SM, NameLoc);
+  Position NameEnd = sourceLocToPosition(
+  SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
 
   index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
   // FIXME: this is not classifying constructors, destructors and operators
@@ -1210,12 +1211,9 @@
   THI.name = printName(Ctx, ND);
   THI.kind = SK;
   THI.deprecated = ND.isDeprecated();
-  THI.range = halfOpenToRange(
-  SM, syntax::Token::range(SM, DeclToks->front(), DeclToks->back())
-  .toCharRange(SM));
-  THI.selectionRange = halfOpenToRange(
-  SM, syntax::Token::range(SM, NameToks->front(), NameToks->back())
-  .toCharRange(SM));
+  THI.range = Range{sourceLocToPosition(SM, DeclRange->getBegin()),
+sourceLocToPosition(SM, DeclRange->getEnd())};
+  THI.selectionRange = Range{NameBegin, NameEnd};
   if (!THI.range.contains(THI.selectionRange)) {
 // 'selectionRange' must be contained in 'range', so in cases where clang
 // reports unrelated ranges we need to reconcile somehow.
@@ -1282,8 +1280,7 @@
 
 static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
std::vector &SuperTypes,
-   RecursionProtectionSet &RPSet,
-   const syntax::TokenBuffer &TB) {
+   RecursionProtectionSet &RPSet) {
   // typeParents() will replace dependent template specializations
   // with their class template, so to avoid infinite recursion for
   // certain types of hierarchies, keep the templates encountered
@@ -1298,9 +1295,9 @@
 
   for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
 if (Optional ParentSym =
-declToTypeHierarchyItem(ASTCtx, *ParentDecl, TB)) {
+declToTypeHierarchyItem(ASTCtx, *Paren

[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked 2 inline comments as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp:533
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct Parent {

kadircet wrote:
> this doesn't need to be an `Annotation` and sorry for missing it in the first 
> pass but since this only has a single header, you can actually do something 
> like:
> 
> ```
> TestTU TU = TestTU::withCode(SourceAnnotations.code());
> TU.HeaderCode = "struct Parent { int a; }";
> ```
> 
> and also drop the include directive in `SourceAnnotations` as `TU.HeaderCode` 
> is implicitly included.
Seems now I need it to check selection range.



Comment at: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp:548
+  EXPECT_THAT(*Result, AllOf(WithName("Child"),
+ Parents(AllOf(WithName("Parent"), Parents();
+}

kadircet wrote:
> could you also make sure selection range is correct at least for `Parent` (as 
> main file ranges are tested elsewhere already), so that we don't regress it 
> in the future.
Added Parent selection range check.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144



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


[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D84144#2161579 , @kadircet wrote:

> also please let me know if I should land this for you.


Yes, could you please land this for me? I do not have commit access.
Aleksandr Platonov 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144



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


[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279180.
ArcsinX added a comment.

AdditionalFiles["header_in_preamble.h"] => HeaderCode


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Index: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
===
--- clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
+++ clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp
@@ -523,6 +523,33 @@
WithKind(SymbolKind::Struct), Children();
 }
 
+TEST(TypeHierarchy, Preamble) {
+  Annotations SourceAnnotations(R"cpp(
+struct Ch^ild : Parent {
+  int b;
+};)cpp");
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct [[Parent]] {
+  int a;
+};)cpp");
+
+  TestTU TU = TestTU::withCode(SourceAnnotations.code());
+  TU.HeaderCode = HeaderInPreambleAnnotations.code().str();
+  auto AST = TU.build();
+
+  llvm::Optional Result = getTypeHierarchy(
+  AST, SourceAnnotations.point(), 1, TypeHierarchyDirection::Parents);
+
+  ASSERT_TRUE(Result);
+  EXPECT_THAT(
+  *Result,
+  AllOf(WithName("Child"),
+Parents(AllOf(WithName("Parent"),
+  SelectionRangeIs(HeaderInPreambleAnnotations.range()),
+  Parents();
+}
+
 SymbolID findSymbolIDByName(SymbolIndex *Index, llvm::StringRef Name,
 llvm::StringRef TemplateArgs = "") {
   SymbolID Result;
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1183,23 +1183,24 @@
 
 // FIXME(nridge): Reduce duplication between this function and declToSym().
 static llvm::Optional
-declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND,
-const syntax::TokenBuffer &TB) {
+declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
   auto &SM = Ctx.getSourceManager();
   SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
+  SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
+  SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
+  const auto DeclRange =
+  toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
+  if (!DeclRange)
+return llvm::None;
   auto FilePath =
   getCanonicalPath(SM.getFileEntryForID(SM.getFileID(NameLoc)), SM);
   auto TUPath = getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
   if (!FilePath || !TUPath)
 return llvm::None; // Not useful without a uri.
 
-  auto DeclToks = TB.spelledForExpanded(TB.expandedTokens(ND.getSourceRange()));
-  if (!DeclToks || DeclToks->empty())
-return llvm::None;
-
-  auto NameToks = TB.spelledForExpanded(TB.expandedTokens(NameLoc));
-  if (!NameToks || NameToks->empty())
-return llvm::None;
+  Position NameBegin = sourceLocToPosition(SM, NameLoc);
+  Position NameEnd = sourceLocToPosition(
+  SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
 
   index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
   // FIXME: this is not classifying constructors, destructors and operators
@@ -1210,12 +1211,9 @@
   THI.name = printName(Ctx, ND);
   THI.kind = SK;
   THI.deprecated = ND.isDeprecated();
-  THI.range = halfOpenToRange(
-  SM, syntax::Token::range(SM, DeclToks->front(), DeclToks->back())
-  .toCharRange(SM));
-  THI.selectionRange = halfOpenToRange(
-  SM, syntax::Token::range(SM, NameToks->front(), NameToks->back())
-  .toCharRange(SM));
+  THI.range = Range{sourceLocToPosition(SM, DeclRange->getBegin()),
+sourceLocToPosition(SM, DeclRange->getEnd())};
+  THI.selectionRange = Range{NameBegin, NameEnd};
   if (!THI.range.contains(THI.selectionRange)) {
 // 'selectionRange' must be contained in 'range', so in cases where clang
 // reports unrelated ranges we need to reconcile somehow.
@@ -1282,8 +1280,7 @@
 
 static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
std::vector &SuperTypes,
-   RecursionProtectionSet &RPSet,
-   const syntax::TokenBuffer &TB) {
+   RecursionProtectionSet &RPSet) {
   // typeParents() will replace dependent template specializations
   // with their class template, so to avoid infinite recursion for
   // certain types of hierarchies, keep the templates encountered
@@ -1298,9 +1295,9 @@
 
   for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
 if (Optional ParentSym =
-declToTypeHierarchyItem(ASTCtx, *ParentDecl, TB)) {
+declToTypeHierarchyItem(ASTCtx, *ParentDecl)) {
   ParentSym->parents.emplace()

[PATCH] D84144: [clangd] Remove TokenBuffer usage in TypeHierarchy

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp:533
+
+  Annotations HeaderInPreambleAnnotations(R"cpp(
+struct Parent {

kadircet wrote:
> ArcsinX wrote:
> > kadircet wrote:
> > > this doesn't need to be an `Annotation` and sorry for missing it in the 
> > > first pass but since this only has a single header, you can actually do 
> > > something like:
> > > 
> > > ```
> > > TestTU TU = TestTU::withCode(SourceAnnotations.code());
> > > TU.HeaderCode = "struct Parent { int a; }";
> > > ```
> > > 
> > > and also drop the include directive in `SourceAnnotations` as 
> > > `TU.HeaderCode` is implicitly included.
> > Seems now I need it to check selection range.
> right, you need annotations for the range, but you still can get away with 
> just setting `TU.HeaderCode` instead of populating AdditionalFiles and 
> including the header in the source. but not that important.
Thanks. fixed


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84144



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


[PATCH] D83759: [clangd] Port lit tests to Windows

2020-07-20 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

What do you think of this patch? I'm not sure if Windows is important OS for 
developers, and that some of these changes might be awkward to maintain, but 
maybe we can enable `background-index.test` at least?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Port lit tests to Windows

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/test/background-index.test:18
 # Test that the index is writing files in the expected location.
 # RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
 # RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx

kadircet wrote:
> are you sure these don't need changes on windows bots ? AFAICT this contains 
> both backslashes(coming from %t) and forwards slashes, and command is `ls` 
> which might not be available on a default windows prompt.
I am not sure. How I can check it? The only thing I can say, that I tested this 
on mingw and visual studio builds on my local machine.

>  this contains both backslashes(coming from %t) and forwards slashes,
For me that was OK, but I agree that `ls %t/.cache/clangd/index/foo.cpp.*.idx` 
should be replaced with `ls %/t/.cache/clangd/index/foo.cpp.*.idx`.

As for `ls`, it's a part of mingw, so I think `ls` absence probability is the 
same as for `sed`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Port lit tests to Windows

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279482.
ArcsinX added a comment.

Revert tests split; Replace '\' with '/' in background-index.test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -i -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' 
%/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck -strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s

[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83759#2164296 , @kadircet wrote:

> as i mentioned please watch out for buildbot breakages after landing this. 
> (and again let me know if I should land this for you)


Thanks for review.

Yes, could you please land this for me?
Aleksandr Platonov 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGff63d6be93dc: [clangd] Fixes in lit tests (authored by 
ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -i -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' 
%/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck -strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR 

[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83759#2165116 , @thakis wrote:

> Looks like this breaks tests on macOS (and probably with non-gnu greps): 
> http://45.33.8.238/mac/17611/step_9.txt
>
> Please take a look, and revert while you investigate if it takes a while to 
> fix.


Reverted.

Seems the problem is in `sed`  without gnu extensions, but I wonder how other 
tests with similar `sed` command could pass (e.g. 
`compile-commands-path-in-initialize.test`)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[Differential] D83759: [clangd] Port lit tests to Windows

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rGff63d6be93dc: [clangd] Fixes in lit tests (authored by 
ArcsinX).

Changed prior to commit:
  https://reviews.llvm.org/D83759?vs=277768&id=279060#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc
+# RUN: sed -i -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -i -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' 
%/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clan

[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-21 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

Seem the problem is in `-i` option.
According with OSX man:
`sed [-Ealn] command [file ... ]`
`sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...]`.

Seems on macOS `-E` is treated as an argument for `-i` in command like `sed -i 
-E -e ...` .

Also, seems commands like `sed -i -e ...` are unsafe on macOS. Such commands 
will create backup with name `-e`, but I do not have macOS to check 
it.
@thakis could you please verify that after tests you have extra  `-e` 
files in 
`/Users/thakis/src/llvm-project/out/gn/obj/clang-tools-extra/clangd/test/Output/background-index.test.tmp/`
 ?

I think we could avoid `-i` usage to fix this problem.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279754.
ArcsinX added a comment.

[macOS] Fix `background-index.test` failure, don't create `*-e` files during 
`background-index.test`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc > 
%/t/definition.jsonrpc.1
+# RUN: sed -i.bak -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' 
%/t/definition.jsonrpc.1 > %/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck -strict-whitespace %s
-# RUN: cat %t | 

[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

I believe it's okay now on macOS.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/test/background-index.test:6
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc > 
%/t/definition.jsonrpc.1
+# RUN: sed -i.bak -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."

kadircet wrote:
> i don't think there's much point in dropping `-i` from only some commands.
> 
> I would suggest either:
> - dropping it from all, by changing the original files to have a `.tmpl` 
> suffix, and dropping the suffix in sed'd versions
> - keeping it for all comments by adding a `-i.bak` to all
For `definition.jsonrpc` we have two `sed` commands and we can keep the file 
name unchanged without `-i` option.
But for `compile_commands.json` we have only one `sed` command and we need `-i` 
option to keep the file name unchanged.


> changing the original files to have a .tmpl suffix
In other tests `.1` suffix is used, that's why I use this suffix here.
E.g. `compile-commands-path-in-initialize.test`
```
# RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1
...
# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %t.test.1 > %t.test
```


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 279781.
ArcsinX added a comment.

Get rid of `-i` `sed` option


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/Inputs/background-index/compile_commands.json
  
clang-tools-extra/clangd/test/Inputs/background-index/compile_commands.json.tmpl
  clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc
  clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc.tmpl
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test

Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck -strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc.tmpl > %/t/definition.jsonrpc.1
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/compile_commands.json.tmpl > %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %/t/definition.jsonrpc.1 > %/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck %t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck %/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck %t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck %/t/definition.jsonrpc --check-prefixes=CHECK,USE
Index: clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc
===
--- /dev/null
+++ clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc
@@ -1,76 +0,0 @@
-{
-  "jsonrpc": "2.0",
-  "id": 0,
-  "method": "initialize",
-  "params": {
-"processId": 123,
-"rootPath": "clangd",
-"capabilities": { "window": { "workDoneProgress": true, "implicitWorkDoneProgressCreate": true} },
-"trace": "off"
-  }
-}

-{
-  "jsonrpc": "2.0",
-  "method": "textDocument/didOpen",
-  "params": {
-"textDocument": {
-  "uri": "file://DIRECTORY/bar.cpp",
-  "languageId": "cpp",
-  "version": 1,
- 

[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/test/background-index.test:6
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc > 
%/t/definition.jsonrpc.1
+# RUN: sed -i.bak -e "s|DIRECTORY|%/t|" %/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."

kadircet wrote:
> ArcsinX wrote:
> > kadircet wrote:
> > > i don't think there's much point in dropping `-i` from only some commands.
> > > 
> > > I would suggest either:
> > > - dropping it from all, by changing the original files to have a `.tmpl` 
> > > suffix, and dropping the suffix in sed'd versions
> > > - keeping it for all comments by adding a `-i.bak` to all
> > For `definition.jsonrpc` we have two `sed` commands and we can keep the 
> > file name unchanged without `-i` option.
> > But for `compile_commands.json` we have only one `sed` command and we need 
> > `-i` option to keep the file name unchanged.
> > 
> > 
> > > changing the original files to have a .tmpl suffix
> > In other tests `.1` suffix is used, that's why I use this suffix here.
> > E.g. `compile-commands-path-in-initialize.test`
> > ```
> > # RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1
> > ...
> > # RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %t.test.1 > %t.test
> > ```
> > For definition.jsonrpc we have two sed commands and we can keep the file 
> > name unchanged without -i option.
> 
> yes for that one we could still move into a `.1` (or `.tmp`) suffixed version.
> 
> > In other tests .1 suffix is used, that's why I use this suffix here.
> 
> right, I was talking about renaming 
> `clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc` to 
> `clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc.tmpl`
>  (likewise the compile_commands.json). That way you can always do a sed, and 
> write to a different file (i.e. the version without the suffix).
Got it, fixed, thanks.



Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759



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


[PATCH] D83759: [clangd] Fixes in lit tests

2020-07-22 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG98b56c09be00: [clangd] Fixes in lit tests (authored by 
ArcsinX).

Changed prior to commit:
  https://reviews.llvm.org/D83759?vs=279781&id=279789#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/Inputs/background-index/compile_commands.json
  
clang-tools-extra/clangd/test/Inputs/background-index/compile_commands.json.tmpl
  clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc
  clang-tools-extra/clangd/test/Inputs/background-index/definition.jsonrpc.tmpl
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: clang-tools-extra/clangd/test/background-index.test
===
--- clang-tools-extra/clangd/test/background-index.test
+++ clang-tools-extra/clangd/test/background-index.test
@@ -1,23 +1,23 @@
-# We need to splice paths into file:// URIs for this test.
-# UNSUPPORTED: windows-msvc
-
 # Use a copy of inputs, as we'll mutate it (as will the background index).
-# RUN: rm -rf %t
-# RUN: cp -r %S/Inputs/background-index %t
+# RUN: rm -rf %/t
+# RUN: cp -r %/S/Inputs/background-index %/t
 # Need to embed the correct temp path in the actual JSON-RPC requests.
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/definition.jsonrpc
-# RUN: sed -i -e "s|DIRECTORY|%t|" %t/compile_commands.json
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/definition.jsonrpc.tmpl > 
%/t/definition.jsonrpc.1
+# RUN: sed -e "s|DIRECTORY|%/t|" %/t/compile_commands.json.tmpl > 
%/t/compile_commands.json
+# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
+# (with the extra slash in the front), so we add it here.
+# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' 
%/t/definition.jsonrpc.1 > %/t/definition.jsonrpc
 
 # We're editing bar.cpp, which includes foo.h.
 # foo() is declared in foo.h and defined in foo.cpp.
 # The background index should allow us to go-to-definition on foo().
 # We should also see indexing progress notifications.
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,BUILD
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,BUILD
 
 # Test that the index is writing files in the expected location.
-# RUN: ls %t/.cache/clangd/index/foo.cpp.*.idx
-# RUN: ls %t/sub_dir/.cache/clangd/index/foo.h.*.idx
+# RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx
+# RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx
 
 # Test the index is read from disk: delete code and restart clangd.
-# RUN: rm %t/foo.cpp
-# RUN: clangd -background-index -lit-test < %t/definition.jsonrpc | FileCheck 
%t/definition.jsonrpc --check-prefixes=CHECK,USE
+# RUN: rm %/t/foo.cpp
+# RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck 
%/t/definition.jsonrpc --check-prefixes=CHECK,USE


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}

[PATCH] D84513: [clangd] Collect references for externally visible main-file symbols

2020-07-24 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, 
MaskRay, ilya-biryukov.
Herald added a project: clang.

Without this patch clangd does not collect references for main-file symbols if 
there is no public declaration in preamble.
Example:
`test1.c`

  void f1() {}

`test2.c`

  extern void f1();
  void f2() {
f^1();
  }

`Find all references` does not show definition of f1() in the result, but GTD 
works OK.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84513

Files:
  clang-tools-extra/clangd/index/Serialization.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -624,11 +624,13 @@
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(Symbols, "NS").ID, _;
   EXPECT_THAT(Refs, Contains(Pair(findSymbol(Symbols, "MACRO").ID,
   HaveRanges(Main.ranges("macro");
-  // Symbols *only* in the main file (a, b, c, FUNC) had no refs collected.
+  // Symbols *only* in the main file:
+  // - (a, b) externally visible and should have refs.
+  // - (c, FUNC) externally invisible and had no refs collected.
   auto MainSymbols =
   TestTU::withHeaderCode(SymbolsOnlyInMainCode.code()).headerSymbols();
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "a").ID, _;
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "b").ID, _;
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "a").ID, _)));
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "b").ID, _)));
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "c").ID, _;
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "FUNC").ID, 
_;
 }
@@ -816,11 +818,15 @@
 $Foo[[Foo]] fo;
   }
   )");
-  // The main file is normal .cpp file, we shouldn't collect any refs of 
symbols
-  // which are not declared in the preamble.
+  // The main file is normal .cpp file, we should collect the refs
+  // for externally visible symbols.
   TestFileName = testPath("foo.cpp");
   runSymbolCollector("", Header.code());
-  EXPECT_THAT(Refs, UnorderedElementsAre());
+  EXPECT_THAT(Refs,
+  UnorderedElementsAre(Pair(findSymbol(Symbols, "Foo").ID,
+HaveRanges(Header.ranges("Foo"))),
+   Pair(findSymbol(Symbols, "Func").ID,
+HaveRanges(Header.ranges("Func");
 
   // Run the .h file as main file, we should collect the refs.
   TestFileName = testPath("foo.h");
Index: clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
@@ -171,7 +171,7 @@
   #endif
   )cpp";
   FS.Files[testPath("root/A.cc")] =
-  "#include \"A.h\"\nvoid g() { (void)common; }";
+  "#include \"A.h\"\nstatic void g() { (void)common; }";
   FS.Files[testPath("root/B.cc")] =
   R"cpp(
   #define A 0
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -314,7 +314,8 @@
   // file locations for references (as it aligns the behavior of clangd's
   // AST-based xref).
   // FIXME: we should try to use the file locations for other fields.
-  if (CollectRef && !IsMainFileOnly && !isa(ND) &&
+  if (CollectRef && (!IsMainFileOnly || ND->isExternallyVisible()) &&
+  !isa(ND) &&
   (Opts.RefsInHeaders ||
SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID()))
 DeclRefs[ND].emplace_back(SM.getFileLoc(Loc), Roles);
Index: clang-tools-extra/clangd/index/Serialization.cpp
===
--- clang-tools-extra/clangd/index/Serialization.cpp
+++ clang-tools-extra/clangd/index/Serialization.cpp
@@ -419,7 +419,7 @@
 // The current versioning scheme is simple - non-current versions are rejected.
 // If you make a breaking change, bump this version number to invalidate stored
 // data. Later we may want to support some backward compatibility.
-constexpr static uint32_t Version = 13;
+constexpr static uint32_t Version = 14;
 
 llvm::Expected readRIFF(llvm::StringRef Data) {
   auto RIFF = riff::readFile(Data);


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-e

[PATCH] D84513: [clangd] Collect references for externally visible main-file symbols

2020-07-26 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 280713.
ArcsinX added a comment.

Remove version bump


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84513

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -624,11 +624,13 @@
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(Symbols, "NS").ID, _;
   EXPECT_THAT(Refs, Contains(Pair(findSymbol(Symbols, "MACRO").ID,
   HaveRanges(Main.ranges("macro");
-  // Symbols *only* in the main file (a, b, c, FUNC) had no refs collected.
+  // Symbols *only* in the main file:
+  // - (a, b) externally visible and should have refs.
+  // - (c, FUNC) externally invisible and had no refs collected.
   auto MainSymbols =
   TestTU::withHeaderCode(SymbolsOnlyInMainCode.code()).headerSymbols();
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "a").ID, _;
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "b").ID, _;
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "a").ID, _)));
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "b").ID, _)));
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "c").ID, _;
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "FUNC").ID, 
_;
 }
@@ -816,11 +818,15 @@
 $Foo[[Foo]] fo;
   }
   )");
-  // The main file is normal .cpp file, we shouldn't collect any refs of 
symbols
-  // which are not declared in the preamble.
+  // The main file is normal .cpp file, we should collect the refs
+  // for externally visible symbols.
   TestFileName = testPath("foo.cpp");
   runSymbolCollector("", Header.code());
-  EXPECT_THAT(Refs, UnorderedElementsAre());
+  EXPECT_THAT(Refs,
+  UnorderedElementsAre(Pair(findSymbol(Symbols, "Foo").ID,
+HaveRanges(Header.ranges("Foo"))),
+   Pair(findSymbol(Symbols, "Func").ID,
+HaveRanges(Header.ranges("Func");
 
   // Run the .h file as main file, we should collect the refs.
   TestFileName = testPath("foo.h");
Index: clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
@@ -171,7 +171,7 @@
   #endif
   )cpp";
   FS.Files[testPath("root/A.cc")] =
-  "#include \"A.h\"\nvoid g() { (void)common; }";
+  "#include \"A.h\"\nstatic void g() { (void)common; }";
   FS.Files[testPath("root/B.cc")] =
   R"cpp(
   #define A 0
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -314,7 +314,8 @@
   // file locations for references (as it aligns the behavior of clangd's
   // AST-based xref).
   // FIXME: we should try to use the file locations for other fields.
-  if (CollectRef && !IsMainFileOnly && !isa(ND) &&
+  if (CollectRef && (!IsMainFileOnly || ND->isExternallyVisible()) &&
+  !isa(ND) &&
   (Opts.RefsInHeaders ||
SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID()))
 DeclRefs[ND].emplace_back(SM.getFileLoc(Loc), Roles);


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -624,11 +624,13 @@
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(Symbols, "NS").ID, _;
   EXPECT_THAT(Refs, Contains(Pair(findSymbol(Symbols, "MACRO").ID,
   HaveRanges(Main.ranges("macro");
-  // Symbols *only* in the main file (a, b, c, FUNC) had no refs collected.
+  // Symbols *only* in the main file:
+  // - (a, b) externally visible and should have refs.
+  // - (c, FUNC) externally invisible and had no refs collected.
   auto MainSymbols =
   TestTU::withHeaderCode(SymbolsOnlyInMainCode.code()).headerSymbols();
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "a").ID, _;
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "b").ID, _;
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "a").ID, _)));
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "b").ID, _)));
   EXPECT_THAT(Refs, Not(Con

[PATCH] D84513: [clangd] Collect references for externally visible main-file symbols

2020-07-26 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D84513#2172028 , @kadircet wrote:

> thanks, this looks good in general, but it would be nice to make sure we are 
> not blowing the index memory and disk usage.
>
> could you grab some numbers for these two before/after this patch?


Project: LLVM with clang and clang-tools-extra (~3700 records in 
compile_commands.json)

**Before:**
Index size on disk: 96461310 bytes
BackgroundIndex: serving version 39 (596622326 bytes)

**After:**
Index size of disk: 97425346 bytes.
BackgroundIndex: serving version 39 (601094341 bytes)

**Diff:**
Size on disk: +1%
Memory:   +0.7%

> this isn't really necessary as we are not changing the serialization format. 
> you would need to delete the existing clangd caches from your machine instead.

Removed version bump.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84513



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


[PATCH] D84513: [clangd] Collect references for externally visible main-file symbols

2020-07-27 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG90684d154516: [clangd] Collect references for externally 
visible main-file symbols (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84513

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -624,11 +624,13 @@
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(Symbols, "NS").ID, _;
   EXPECT_THAT(Refs, Contains(Pair(findSymbol(Symbols, "MACRO").ID,
   HaveRanges(Main.ranges("macro");
-  // Symbols *only* in the main file (a, b, c, FUNC) had no refs collected.
+  // Symbols *only* in the main file:
+  // - (a, b) externally visible and should have refs.
+  // - (c, FUNC) externally invisible and had no refs collected.
   auto MainSymbols =
   TestTU::withHeaderCode(SymbolsOnlyInMainCode.code()).headerSymbols();
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "a").ID, _;
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "b").ID, _;
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "a").ID, _)));
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "b").ID, _)));
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "c").ID, _;
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "FUNC").ID, 
_;
 }
@@ -816,11 +818,15 @@
 $Foo[[Foo]] fo;
   }
   )");
-  // The main file is normal .cpp file, we shouldn't collect any refs of 
symbols
-  // which are not declared in the preamble.
+  // The main file is normal .cpp file, we should collect the refs
+  // for externally visible symbols.
   TestFileName = testPath("foo.cpp");
   runSymbolCollector("", Header.code());
-  EXPECT_THAT(Refs, UnorderedElementsAre());
+  EXPECT_THAT(Refs,
+  UnorderedElementsAre(Pair(findSymbol(Symbols, "Foo").ID,
+HaveRanges(Header.ranges("Foo"))),
+   Pair(findSymbol(Symbols, "Func").ID,
+HaveRanges(Header.ranges("Func");
 
   // Run the .h file as main file, we should collect the refs.
   TestFileName = testPath("foo.h");
Index: clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
@@ -171,7 +171,7 @@
   #endif
   )cpp";
   FS.Files[testPath("root/A.cc")] =
-  "#include \"A.h\"\nvoid g() { (void)common; }";
+  "#include \"A.h\"\nstatic void g() { (void)common; }";
   FS.Files[testPath("root/B.cc")] =
   R"cpp(
   #define A 0
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -314,7 +314,8 @@
   // file locations for references (as it aligns the behavior of clangd's
   // AST-based xref).
   // FIXME: we should try to use the file locations for other fields.
-  if (CollectRef && !IsMainFileOnly && !isa(ND) &&
+  if (CollectRef && (!IsMainFileOnly || ND->isExternallyVisible()) &&
+  !isa(ND) &&
   (Opts.RefsInHeaders ||
SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID()))
 DeclRefs[ND].emplace_back(SM.getFileLoc(Loc), Roles);


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -624,11 +624,13 @@
   EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(Symbols, "NS").ID, _;
   EXPECT_THAT(Refs, Contains(Pair(findSymbol(Symbols, "MACRO").ID,
   HaveRanges(Main.ranges("macro");
-  // Symbols *only* in the main file (a, b, c, FUNC) had no refs collected.
+  // Symbols *only* in the main file:
+  // - (a, b) externally visible and should have refs.
+  // - (c, FUNC) externally invisible and had no refs collected.
   auto MainSymbols =
   TestTU::withHeaderCode(SymbolsOnlyInMainCode.code()).headerSymbols();
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "a").ID, _;
-  EXPECT_THAT(Refs, Not(Contains(Pair(findSymbol(MainSymbols, "b").ID, _;
+  EXPECT_THAT(Refs, Contains(Pair(findSymbol(MainSymbols, "a").ID, _

[PATCH] D84912: [clangd] findNearbyIdentifier(): fix the word search in the token stream.

2020-07-30 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous.
Herald added a project: clang.
ArcsinX requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

Without this patch the word occurrence search always returns the first token of 
the file.
Despite of that, `findNeardyIdentifier()` returns the correct result (but 
inefficently) until there are several matched tokens with the same value 
`floor(log2( - ))` (e.g. several matched tokens on the 
same line).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84912

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1197,7 +1197,14 @@
 
   // h^i
 )cpp",
-  };
+  R"cpp(
+  // prefer nearest occurrence even if several matched tokens
+  // have the same value of `floor(log2( - ))`.
+  int hello;
+  int x = hello, y = hello;
+  int z = [[hello]];
+  // h^ello
+)cpp"};
   for (const char *Test : Tests) {
 Annotations T(Test);
 auto AST = TestTU::withCode(T.code()).build();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -518,7 +518,7 @@
   // Find where the word occurred in the token stream, to search forward & 
back.
   auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
 assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
-return T.location() >= Word.Location; // Comparison OK: same file.
+return T.location() < Word.Location; // Comparison OK: same file.
   });
   // Search for matches after the cursor.
   for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1197,7 +1197,14 @@
 
   // h^i
 )cpp",
-  };
+  R"cpp(
+  // prefer nearest occurrence even if several matched tokens
+  // have the same value of `floor(log2( - ))`.
+  int hello;
+  int x = hello, y = hello;
+  int z = [[hello]];
+  // h^ello
+)cpp"};
   for (const char *Test : Tests) {
 Annotations T(Test);
 auto AST = TestTU::withCode(T.code()).build();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -518,7 +518,7 @@
   // Find where the word occurred in the token stream, to search forward & back.
   auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
 assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
-return T.location() >= Word.Location; // Comparison OK: same file.
+return T.location() < Word.Location; // Comparison OK: same file.
   });
   // Search for matches after the cursor.
   for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D84912: [clangd] findNearbyIdentifier(): fix the word search in the token stream.

2020-07-30 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D84912#2184147 , @kadircet wrote:

> oh and also, how did you notice the discrepancy? was it a random encounter 
> while reading the code or did you notice a misbehaviour?

Case was like this

  void f1(int [[paramValue]]){
  }
  
  void f2(int paramValue){
  }
  
  void f3(int paramValue){
  }
  
  void f4(int paramValue){
  // param^Value
  }

Because of
floor(log2(8)) = floor(log2(8)) ... = floor(log2(15)) = 3


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84912

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


[PATCH] D84912: [clangd] findNearbyIdentifier(): fix the word search in the token stream.

2020-07-30 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG05b173466142: [clangd] findNearbyIdentifier(): fix the word 
search in the token stream. (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84912

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1197,7 +1197,14 @@
 
   // h^i
 )cpp",
-  };
+  R"cpp(
+  // prefer nearest occurrence even if several matched tokens
+  // have the same value of `floor(log2( - ))`.
+  int hello;
+  int x = hello, y = hello;
+  int z = [[hello]];
+  // h^ello
+)cpp"};
   for (const char *Test : Tests) {
 Annotations T(Test);
 auto AST = TestTU::withCode(T.code()).build();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -518,7 +518,7 @@
   // Find where the word occurred in the token stream, to search forward & 
back.
   auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
 assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
-return T.location() >= Word.Location; // Comparison OK: same file.
+return T.location() < Word.Location; // Comparison OK: same file.
   });
   // Search for matches after the cursor.
   for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1197,7 +1197,14 @@
 
   // h^i
 )cpp",
-  };
+  R"cpp(
+  // prefer nearest occurrence even if several matched tokens
+  // have the same value of `floor(log2( - ))`.
+  int hello;
+  int x = hello, y = hello;
+  int z = [[hello]];
+  // h^ello
+)cpp"};
   for (const char *Test : Tests) {
 Annotations T(Test);
 auto AST = TestTU::withCode(T.code()).build();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -518,7 +518,7 @@
   // Find where the word occurred in the token stream, to search forward & back.
   auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
 assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
-return T.location() >= Word.Location; // Comparison OK: same file.
+return T.location() < Word.Location; // Comparison OK: same file.
   });
   // Search for matches after the cursor.
   for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D84912: [clangd] findNearbyIdentifier(): fix the word search in the token stream.

2020-07-30 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D84912#2184144 , @kadircet wrote:

> I think we should also cherry-pick this into release branch. I've filed 
> https://bugs.llvm.org/show_bug.cgi?id=46905 for cherry-picking please update 
> the bug
> with the commit hash once you land the fix (or let me know if you don't have 
> an account in bugzilla)

I do not have bugzilla account, so could you please do this for me?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D84912

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


[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-07-01 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked 3 inline comments as done.
ArcsinX added inline comments.



Comment at: clang/lib/Sema/SemaInit.cpp:4539
+if (CXXRecordDecl *T2RecordDecl =
+dyn_cast(T2RecordType->getDecl())) {
+  const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();

riccibruno wrote:
> ArcsinX wrote:
> > riccibruno wrote:
> > > I cannot reproduce this with the provided test case. Can you explain why 
> > > `T2RecordDecl` would not be a `CXXRecordDecl` since we should only get 
> > > there with c++?
> > You can reproduce it with
> > ```
> > bin/clang -xc --target=arm-unknown-gnu -c ../clang/test/Sema/init-ref-c.c
> > ```
> > 
> > We can get there with C in case of using builtins declared as functions 
> > with reference parameters (e.g. `__builtin_va_end()`)
> Okay I see it now. Thank you.
> 
> I think it would be better to only call `TryRefInitWithConversionFunction` in 
> C++ mode since conversion functions don't exist in C (only the first call on 
> line 4773).
Agree. Fixed.



Comment at: clang/lib/Sema/SemaInit.cpp:4706
Qualifiers T2Quals,
InitializationSequence &Sequence) {
   QualType DestType = Entity.getType();

riccibruno wrote:
> Can you add a comment here explaining that we can get there in C with some 
> builtins?
Added extra description for TryReferenceInitializationCore() with explanation 
that we can get here in C.



Comment at: clang/lib/Sema/SemaInit.cpp:4773
 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
 (isLValueRef || InitCategory.isRValue())) {
   ConvOvlResult = TryRefInitWithConversionFunction(

riccibruno wrote:
> Here
Done.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805



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


[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-07-01 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 274789.
ArcsinX added a comment.

Try conversion function only in C++.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-ref-c.c


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue 
reference to type '__builtin_va_list' cannot bind to a value of unrelated type 
'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4693,6 +4693,9 @@
 }
 
 /// Reference initialization without resolving overloaded functions.
+///
+/// We also can get here in C if we call builtin which is declared as
+/// a function with reference arguments (e.g. __builtin_va_end()).
 static void TryReferenceInitializationCore(Sema &S,
const InitializedEntity &Entity,
const InitializationKind &Kind,
@@ -4769,15 +4772,20 @@
 // an rvalue. DR1287 removed the "implicitly" here.
 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
 (isLValueRef || InitCategory.isRValue())) {
-  ConvOvlResult = TryRefInitWithConversionFunction(
-  S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef,
-  /*IsLValueRef*/ isLValueRef, Sequence);
-  if (ConvOvlResult == OR_Success)
-return;
-  if (ConvOvlResult != OR_No_Viable_Function)
-Sequence.SetOverloadFailure(
-InitializationSequence::FK_ReferenceInitOverloadFailed,
-ConvOvlResult);
+  if (S.getLangOpts().CPlusPlus) {
+// Try conversion functions only for C++
+ConvOvlResult = TryRefInitWithConversionFunction(
+S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef,
+/*IsLValueRef*/ isLValueRef, Sequence);
+if (ConvOvlResult == OR_Success)
+  return;
+if (ConvOvlResult != OR_No_Viable_Function)
+  Sequence.SetOverloadFailure(
+  InitializationSequence::FK_ReferenceInitOverloadFailed,
+  ConvOvlResult);
+  } else {
+ConvOvlResult = OR_No_Viable_Function;
+  }
 }
   }
 


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a value of unrelated type 'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4693,6 +4693,9 @@
 }
 
 /// Reference initialization without resolving overloaded functions.
+///
+/// We also can get here in C if we call builtin which is declared as
+/// a function with reference arguments (e.g. __builtin_va_end()).
 static void TryReferenceInitializationCore(Sema &S,
const InitializedEntity &Entity,
const InitializationKind &Kind,
@@ -4769,15 +4772,20 @@
 // an rvalue. DR1287 removed the "implicitly" here.
 if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
 (isLValueRef || InitCategory.isRValue())) {
-  ConvOvlResult = TryRefInitWithConversionFunction(
-  S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef,
-  /*IsLValueRef*/ isLValueRef, Sequence);
-  if (ConvOvlResult == OR_Success)
-return;
-  if (ConvOvlResult != OR_No_Viable_Function)
-Sequence.SetOverloadFailure(
-InitializationSequence::FK_ReferenceInitOverloadFailed,
-ConvOvlResult);
+  if (S.getLangOpts().CPlusPlus) {
+// Try conversion functions only for C++
+ConvOvlResult = TryRefInitWithConversionFunction(
+S, Entity, Kind, Initializer, /*AllowRValues*/ isRValueRef,
+/*IsLValueRef*/ isLValueRef, Sequence);
+if (ConvOvlResult == OR_Success)
+  return;
+if (ConvOvlResult != OR_No_Viable_Function)
+  Sequence.SetOverloadFailure(
+  InitializationSequence::FK_ReferenceInitOverloadFailed,
+  ConvOvlResult);
+  } else {
+  

[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-07-08 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805



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


[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-07-08 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D82805#2139243 , @riccibruno wrote:

> Do you want me to commit it for you?


Yes, thank you.

>   If so I need a name and an email for the attribution.

Aleksandr Platonov 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805



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


[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-09 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, 
MaskRay, ilya-biryukov.
Herald added a project: clang.

This patch fixes redundant hover with information about Decl which is not under 
cursor.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83508

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -942,6 +942,13 @@
   template  void foo() {
 (void)[[size^of]](T);
   })cpp",
+  R"cpp(// No Decl for token under cursor.
+  namespace ns {
+  #define FOO B^AR;
+  })cpp",
+  R"cpp(//error-ok
+  unknown f(i^nt);
+  )cpp",
   // literals
   "auto x = t^rue;",
   "auto x = '^A';",
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -820,6 +820,18 @@
 SelectionTree::createRight(AST.getASTContext(), TB, Offset, Offset);
 std::vector Result;
 if (const SelectionTree::Node *N = ST.commonAncestor()) {
+  // Don't allow Decls which are not related with tokens under cursor.
+  if (const auto *D = N->ASTNode.get()) {
+bool IsDeclTouchingCursor = false;
+for (const auto &Tok : TokensTouchingCursor) {
+  if (D->getLocation() == Tok.location()) {
+IsDeclTouchingCursor = true;
+break;
+  }
+}
+if (!IsDeclTouchingCursor)
+  return llvm::None;
+  }
   // FIXME: Fill in HighlightRange with range coming from N->ASTNode.
   auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias);
   if (!Decls.empty()) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -942,6 +942,13 @@
   template  void foo() {
 (void)[[size^of]](T);
   })cpp",
+  R"cpp(// No Decl for token under cursor.
+  namespace ns {
+  #define FOO B^AR;
+  })cpp",
+  R"cpp(//error-ok
+  unknown f(i^nt);
+  )cpp",
   // literals
   "auto x = t^rue;",
   "auto x = '^A';",
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -820,6 +820,18 @@
 SelectionTree::createRight(AST.getASTContext(), TB, Offset, Offset);
 std::vector Result;
 if (const SelectionTree::Node *N = ST.commonAncestor()) {
+  // Don't allow Decls which are not related with tokens under cursor.
+  if (const auto *D = N->ASTNode.get()) {
+bool IsDeclTouchingCursor = false;
+for (const auto &Tok : TokensTouchingCursor) {
+  if (D->getLocation() == Tok.location()) {
+IsDeclTouchingCursor = true;
+break;
+  }
+}
+if (!IsDeclTouchingCursor)
+  return llvm::None;
+  }
   // FIXME: Fill in HighlightRange with range coming from N->ASTNode.
   auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias);
   if (!Decls.empty()) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D83508: [clangd][Hover] Don't use Decl if it is not related with tokens under cursor.

2020-07-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

@kadircet Thanks for your reply.

Think that you are right, because the same problem appears in GTD (with the 
same test cases). And as for GTD the similar workaround also does not break any 
regression tests except override/final.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83508



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


[PATCH] D83548: [clangd] Fix tests build for GCC5

2020-07-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, 
MaskRay, ilya-biryukov.
Herald added a project: clang.

Build log:

  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp: In member 
function ‘virtual void 
clang::clangd::{anonymous}::PreamblePatchTest_Define_Test::TestBody()’:
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"\012#define BAR\012
[[BAR]]"’ from ‘const char*’ to ‘llvm::StringLitera ’
 };
 ^
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"#line 0 \".*main.cpp\"\012#line 
2\012#define BAR\012"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"\012#define BAR \\\012\012  
  [[BAR]]"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"#line 0 \".*main.cpp\"\012#line 
2\012#define BAR\012"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"\012#define \\\012  
  BAR\012[[BAR]]"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:267:3: 
error: could not convert ‘(const char*)"#line 0 \".*main.cpp\"\012#line 
3\012#define BAR\012"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp: In member 
function ‘virtual void 
clang::clangd::{anonymous}::PreamblePatchTest_LocateMacroAtWorks_Test::TestBody()’:
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
 };
 ^
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#define $def^FOO\012
$use^FOO"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#define $def^FOO\012
#undef $use^FOO"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#define $def^FOO\012
#undef FOO\012$use^FOO"’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#define \\\012  
$def^FOO\012$use^FOO"’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#\\\012  
define /* FOO */\\\012  /* FOO */ $def^FOO\012
$use^FOO"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"#define FOO"’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:357:3: 
error: could not convert ‘(const char*)"\012#define BAR\012 
   #define $def^FOO\012$use^FOO"’ from ‘const char*’ to 
‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp: In member 
function ‘virtual void 
clang::clangd::{anonymous}::PreamblePatchTest_RefsToMacros_Test::TestBody()’:
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:445:3: 
error: could not convert ‘(const char*)""’ from ‘const char*’ to 
‘llvm::StringLiteral’
 };
 ^
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:445:3: 
error: could not convert ‘(const char*)"\012#define ^FOO\012
^[[FOO]]"’ from ‘const char*’ to ‘llvm::StringLiteral’
  llvm-project/clang-tools-extra/clangd/unittests/PreambleTests.cpp:445:

[PATCH] D83548: [clangd] Fix tests build for GCC5

2020-07-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

As far as I do not have commit access, could you please commit for me?
Aleksandr Platonov 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83548



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


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-11 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, ilya-biryukov.
Herald added a project: clang.

If there is no record in compile_commands.json, we try to find suitable record 
with `MatchTrie.findEquivalent()` call.
This is very expensive operation with a lot of `llvm::sys::fs::equivalent()` 
calls in some cases.

This patch adds caching for `MatchTrie.findEquivalent()` call result.

Example scenario without this patch:

- compile_commands.json generated at clangd build (contains ~3000 files)..
- it tooks more than 1 second to get compile command for newly created file in 
the root folder of LLVM project.
- we wait for 1 second every time when clangd requests compile command for this 
file (at file change).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83621

Files:
  clang/include/clang/Tooling/JSONCompilationDatabase.h
  clang/lib/Tooling/JSONCompilationDatabase.cpp


Index: clang/lib/Tooling/JSONCompilationDatabase.cpp
===
--- clang/lib/Tooling/JSONCompilationDatabase.cpp
+++ clang/lib/Tooling/JSONCompilationDatabase.cpp
@@ -230,14 +230,28 @@
   SmallString<128> NativeFilePath;
   llvm::sys::path::native(FilePath, NativeFilePath);
 
-  std::string Error;
-  llvm::raw_string_ostream ES(Error);
-  StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES);
-  if (Match.empty())
-return {};
-  const auto CommandsRefI = IndexByFile.find(Match);
-  if (CommandsRefI == IndexByFile.end())
-return {};
+  // Avoid usage of `MatchTrie` if possible.
+  auto CommandsRefI = IndexByFile.find(NativeFilePath);
+  if (CommandsRefI == IndexByFile.end()) {
+llvm::StringRef Match;
+// Try to get cached value.
+auto MatchIt = MatchCache.find(NativeFilePath);
+if (MatchIt == MatchCache.end()) {
+  std::string Error;
+  llvm::raw_string_ostream ES(Error);
+  Match = MatchTrie.findEquivalent(NativeFilePath, ES);
+  // Save into cache even if the match result is empty.
+  MatchCache[NativeFilePath] = Match;
+} else {
+  // Cached value.
+  Match = MatchIt->second;
+}
+if (Match.empty())
+  return {};
+CommandsRefI = IndexByFile.find(Match);
+if (CommandsRefI == IndexByFile.end())
+  return {};
+  }
   std::vector Commands;
   getCommands(CommandsRefI->getValue(), Commands);
   return Commands;
Index: clang/include/clang/Tooling/JSONCompilationDatabase.h
===
--- clang/include/clang/Tooling/JSONCompilationDatabase.h
+++ clang/include/clang/Tooling/JSONCompilationDatabase.h
@@ -129,6 +129,8 @@
   std::vector AllCommands;
 
   FileMatchTrie MatchTrie;
+  // Cache for `MatchTrie`.
+  mutable llvm::StringMap MatchCache;
 
   std::unique_ptr Database;
   JSONCommandLineSyntax Syntax;


Index: clang/lib/Tooling/JSONCompilationDatabase.cpp
===
--- clang/lib/Tooling/JSONCompilationDatabase.cpp
+++ clang/lib/Tooling/JSONCompilationDatabase.cpp
@@ -230,14 +230,28 @@
   SmallString<128> NativeFilePath;
   llvm::sys::path::native(FilePath, NativeFilePath);
 
-  std::string Error;
-  llvm::raw_string_ostream ES(Error);
-  StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES);
-  if (Match.empty())
-return {};
-  const auto CommandsRefI = IndexByFile.find(Match);
-  if (CommandsRefI == IndexByFile.end())
-return {};
+  // Avoid usage of `MatchTrie` if possible.
+  auto CommandsRefI = IndexByFile.find(NativeFilePath);
+  if (CommandsRefI == IndexByFile.end()) {
+llvm::StringRef Match;
+// Try to get cached value.
+auto MatchIt = MatchCache.find(NativeFilePath);
+if (MatchIt == MatchCache.end()) {
+  std::string Error;
+  llvm::raw_string_ostream ES(Error);
+  Match = MatchTrie.findEquivalent(NativeFilePath, ES);
+  // Save into cache even if the match result is empty.
+  MatchCache[NativeFilePath] = Match;
+} else {
+  // Cached value.
+  Match = MatchIt->second;
+}
+if (Match.empty())
+  return {};
+CommandsRefI = IndexByFile.find(Match);
+if (CommandsRefI == IndexByFile.end())
+  return {};
+  }
   std::vector Commands;
   getCommands(CommandsRefI->getValue(), Commands);
   return Commands;
Index: clang/include/clang/Tooling/JSONCompilationDatabase.h
===
--- clang/include/clang/Tooling/JSONCompilationDatabase.h
+++ clang/include/clang/Tooling/JSONCompilationDatabase.h
@@ -129,6 +129,8 @@
   std::vector AllCommands;
 
   FileMatchTrie MatchTrie;
+  // Cache for `MatchTrie`.
+  mutable llvm::StringMap MatchCache;
 
   std::unique_ptr Database;
   JSONCommandLineSyntax Syntax;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-11 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

Also I think that `FileMatchTrie` introduced in https://reviews.llvm.org/D30 is 
not efficient solution to fight with symlinks and for most cases 
`InterpolatingCompilationDatabase` will be accurate enough. But I am not sure, 
is it safe to completely remove `FileMatchTrie`? What do you think?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621



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


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-13 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83621#2146706 , @sammccall wrote:

> Wow, yeah, this seems pretty bad! I'm not very familiar with this code. I'm 
> curious, for "it tooks more than 1 second" - what OS is this on?


Windows.
I faced this problem on a huge project and it takes 7 seconds for 
`MatchTrie.findEquivalent()` to return.

> My guess is that way back when, the performance of looking up a CDB entry 
> that didn't exist wasn't a big deal, or windows support wasn't a big thing, 
> or this was just an oversight.
> 
> I think it would be slightly cleaner to fix this in the trie itself, either:

Firstly, I wanted to fix this problem in `FileMatchTrie`, but for caching 
solution we need to update cache at every `instert()` call, but for caching 
solution inside `JSONCompilationDatabase` we can avoid this, because all 
inserts already done at `JSONCompilationDatabase::parse()`.

> - don't scan for equivalences if the set of candidates exceeds some size 
> threshold (10 or so)
> - don't scan for equivalences if the node we'd scan under is the root

After such fixes `JSONCompilationDatabase::getCompileCommands()` will return 
other results than before in some cases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621



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


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-13 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83621#2146746 , @klimek wrote:

> IIRC the symlink checking was there because some part of the system on linux 
> tends to give us realpaths (with CMake builds), and that leads to not finding 
> anything if there are symlinks involved.


That was before `InterpolatingCompilationDatabase` introduction in 
https://reviews.llvm.org/D45006.
For my experience `InterpolatingCompilationDatabase` works pretty well for 
symlinks cases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621



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


[PATCH] D83759: [clangd] Port lit tests to Windows

2020-07-14 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, 
MaskRay, ilya-biryukov.
Herald added a project: clang.

Changes:

- `background-index.test` Add Windows support.
- `dependency-output.test` Split into two tests (Windows and Posix).
- `did-change-configuration-params.test` Split into two tests (Windows and 
Posix).
- `test-uri-windows.test` This test did not run on Windows displite `REQUIRES: 
windows-gnu || windows-msvc` (replacement: `UNSUPPORTED: !(windows-gnu || 
windows-msvc)`).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83759

Files:
  clang-tools-extra/clangd/test/background-index.test
  clang-tools-extra/clangd/test/dependency-output-posix.test
  clang-tools-extra/clangd/test/dependency-output-windows.test
  clang-tools-extra/clangd/test/dependency-output.test
  clang-tools-extra/clangd/test/did-change-configuration-params-posix.test
  clang-tools-extra/clangd/test/did-change-configuration-params-windows.test
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/test/test-uri-windows.test


Index: clang-tools-extra/clangd/test/test-uri-windows.test
===
--- clang-tools-extra/clangd/test/test-uri-windows.test
+++ clang-tools-extra/clangd/test/test-uri-windows.test
@@ -1,5 +1,5 @@
 # RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
-# REQUIRES: windows-gnu || windows-msvc
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 # Test authority-less URI
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
Index: 
clang-tools-extra/clangd/test/did-change-configuration-params-windows.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params-windows.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params-windows.test
@@ -1,9 +1,9 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
-# UNSUPPORTED: windows-gnu,windows-msvc
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
+# UNSUPPORTED: !(windows-gnu || windows-msvc)
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
-{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabaseChanges":{"/clangd-test/foo.c":
 {"workingDirectory":"/clangd-test", "compilationCommand": ["clang", "-c", 
"foo.c"]}
+{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabaseChanges":{"C:\\clangd-test\\foo.c":
 {"workingDirectory":"C:\\clangd-test", "compilationCommand": ["clang", "-c", 
"foo.c"]}
 ---
 
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.c","languageId":"c","text":"int
 main() { int i; return i; }"}}}
 #  CHECK:  "method": "textDocument/publishDiagnostics",
@@ -21,7 +21,7 @@
 # CHECK-NEXT:"version": 0
 # CHECK-NEXT:  }
 ---
-{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabaseChanges":{"/clangd-test/foo.c":
 {"workingDirectory":"/clangd-test2", "compilationCommand": ["clang", "-c", 
"foo.c", "-Wall", "-Werror"]}
+{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"compilationDatabaseChanges":{"C:\\clangd-test\\foo.c":
 {"workingDirectory":"C:\\clangd-test2", "compilationCommand": ["clang", "-c", 
"foo.c", "-Wall", "-Werror"]}
 #  CHECK:  "method": "textDocument/publishDiagnostics",
 # CHECK-NEXT:  "params": {
 # CHECK-NEXT:"diagnostics": [
@@ -53,5 +53,3 @@
 {"jsonrpc":"2.0","id":5,"method":"shutdown"}
 ---
 {"jsonrpc":"2.0","method":"exit"}
-
-
Index: clang-tools-extra/clangd/test/did-change-configuration-params-posix.test
===
--- clang-tools-extra/clangd/test/did-change-configuration-params-posix.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params-posix.test
@@ -1,5 +1,5 @@
 # RUN: clangd -compile_args_from=lsp -lit-test < %s 2> %t | FileCheck 
-strict-whitespace %s
-# RUN: cat %t | FileCheck --check-prefix=ERR %s
+# RUN: FileCheck --check-prefix=ERR --input-file=%t %s
 # UNSUPPORTED: windows-gnu,windows-msvc
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
 ---
@@ -53,5 +53,3 @@
 {"jsonrpc":"2.0","id":5,"method":"shutdown"}
 ---
 {"jsonrpc":"2.0","method":"exit"}
-
-
Index: clang-tools-extra/clangd/test/dependency-output-windows.test
===
--- clang-tools-extra/clangd/test/dependency-output-windows.test
+++ clang-tools-extra/clangd/test/dependency-output-wind

[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D83621#2151716 , @sammccall wrote:

> In D83621#2146750 , @ArcsinX wrote:
>
> > > - don't scan for equivalences if the set of candidates exceeds some size 
> > > threshold (10 or so)
> > > - don't scan for equivalences if the node we'd scan under is the root
> >
> > After such fixes `JSONCompilationDatabase::getCompileCommands()` will 
> > return other results than before in some cases.
>
>
> Can you elaborate on this - do you think there are reasonable cases where it 
> will give the wrong answer?
>  I can certainly imagine cases where this changes behaviour, e.g. 
> project/foo.cc is a symlink to project/bar.cc, compile_commands contains an 
> entry for foo.cc, we query for bar.cc.
>  But I don't think this was ever really intended to handle arbitrary symlinks 
> that change the names of files, and I do think this is OK to break.


If breaking `project/foo.cc <=> project/bar.cc` is OK, then could we also break 
`some/path/here/foo.cc <=> some/other/path/bar.cc`?

In that case we can just prevent `Comparator.equivalent(FileA,FileB)` calls 
when `llvm::sys::path::filename(FileA) != llvm::sys::path::filename(FileB)`.
I tried this locally:

- it works fast.
- does not break tests.
- easy to add unit test for `FileMatchTrie`.

What do you think?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621



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


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 278161.
ArcsinX added a comment.

Support only directory simlinks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621

Files:
  clang/lib/Tooling/FileMatchTrie.cpp
  clang/unittests/Tooling/CompilationDatabaseTest.cpp


Index: clang/unittests/Tooling/CompilationDatabaseTest.cpp
===
--- clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -281,6 +281,15 @@
   EXPECT_EQ("Cannot resolve relative paths", Error);
 }
 
+TEST_F(FileMatchTrieTest, SingleFile) {
+  Trie.insert("/root/RootFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+  // Add subpath to avoid `if (Children.empty())` special case
+  // which we hit at previous `find()`.
+  Trie.insert("/root/otherpath/OtherFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+}
+
 TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
   std::string ErrorMessage;
   CompileCommand NotFound = findCompileArgsInJsonDatabase(
Index: clang/lib/Tooling/FileMatchTrie.cpp
===
--- clang/lib/Tooling/FileMatchTrie.cpp
+++ clang/lib/Tooling/FileMatchTrie.cpp
@@ -105,8 +105,13 @@
StringRef FileName,
bool &IsAmbiguous,
unsigned ConsumedLength = 0) const {
+// Note: we support only directory symlinks for performance reasons.
 if (Children.empty()) {
-  if (Comparator.equivalent(StringRef(Path), FileName))
+  // As far as we do not support file symlinks we compare
+  // basenames here to avoid expensive request to file system.
+  if (llvm::sys::path::filename(Path) ==
+  llvm::sys::path::filename(FileName) &&
+  Comparator.equivalent(StringRef(Path), FileName))
 return StringRef(Path);
   return {};
 }
@@ -121,6 +126,15 @@
   if (!Result.empty() || IsAmbiguous)
 return Result;
 }
+
+// We failed to find the file with `Children.find()`.
+// If `ConsumedLength` is equal to 0 then we have tried to find the file
+// with it's basename. Thus, we do not have files with the same
+// basename and could return empty result here as far as we
+// do not support file symlinks.
+if (ConsumedLength == 0)
+  return {};
+
 std::vector AllChildren;
 getAll(AllChildren, MatchingChild);
 StringRef Result;


Index: clang/unittests/Tooling/CompilationDatabaseTest.cpp
===
--- clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -281,6 +281,15 @@
   EXPECT_EQ("Cannot resolve relative paths", Error);
 }
 
+TEST_F(FileMatchTrieTest, SingleFile) {
+  Trie.insert("/root/RootFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+  // Add subpath to avoid `if (Children.empty())` special case
+  // which we hit at previous `find()`.
+  Trie.insert("/root/otherpath/OtherFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+}
+
 TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
   std::string ErrorMessage;
   CompileCommand NotFound = findCompileArgsInJsonDatabase(
Index: clang/lib/Tooling/FileMatchTrie.cpp
===
--- clang/lib/Tooling/FileMatchTrie.cpp
+++ clang/lib/Tooling/FileMatchTrie.cpp
@@ -105,8 +105,13 @@
StringRef FileName,
bool &IsAmbiguous,
unsigned ConsumedLength = 0) const {
+// Note: we support only directory symlinks for performance reasons.
 if (Children.empty()) {
-  if (Comparator.equivalent(StringRef(Path), FileName))
+  // As far as we do not support file symlinks we compare
+  // basenames here to avoid expensive request to file system.
+  if (llvm::sys::path::filename(Path) ==
+  llvm::sys::path::filename(FileName) &&
+  Comparator.equivalent(StringRef(Path), FileName))
 return StringRef(Path);
   return {};
 }
@@ -121,6 +126,15 @@
   if (!Result.empty() || IsAmbiguous)
 return Result;
 }
+
+// We failed to find the file with `Children.find()`.
+// If `ConsumedLength` is equal to 0 then we have tried to find the file
+// with it's basename. Thus, we do not have files with the same
+// basename and could return empty result here as far as we
+// do not support file symlinks.
+if (ConsumedLength == 0)
+  return {};
+
 std::vector AllChildren;
 getAll(AllChildren, MatchingChild);
 StringRef Result;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 278230.
ArcsinX added a comment.

Update comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621

Files:
  clang/lib/Tooling/FileMatchTrie.cpp
  clang/unittests/Tooling/CompilationDatabaseTest.cpp


Index: clang/unittests/Tooling/CompilationDatabaseTest.cpp
===
--- clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -281,6 +281,15 @@
   EXPECT_EQ("Cannot resolve relative paths", Error);
 }
 
+TEST_F(FileMatchTrieTest, SingleFile) {
+  Trie.insert("/root/RootFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+  // Add subpath to avoid `if (Children.empty())` special case
+  // which we hit at previous `find()`.
+  Trie.insert("/root/otherpath/OtherFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+}
+
 TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
   std::string ErrorMessage;
   CompileCommand NotFound = findCompileArgsInJsonDatabase(
Index: clang/lib/Tooling/FileMatchTrie.cpp
===
--- clang/lib/Tooling/FileMatchTrie.cpp
+++ clang/lib/Tooling/FileMatchTrie.cpp
@@ -105,8 +105,13 @@
StringRef FileName,
bool &IsAmbiguous,
unsigned ConsumedLength = 0) const {
+// Note: we support only directory symlinks for performance reasons.
 if (Children.empty()) {
-  if (Comparator.equivalent(StringRef(Path), FileName))
+  // As far as we do not support file symlinks, compare
+  // basenames here to avoid request to file system.
+  if (llvm::sys::path::filename(Path) ==
+  llvm::sys::path::filename(FileName) &&
+  Comparator.equivalent(StringRef(Path), FileName))
 return StringRef(Path);
   return {};
 }
@@ -121,6 +126,13 @@
   if (!Result.empty() || IsAmbiguous)
 return Result;
 }
+
+// If `ConsumedLength` is zero, this is the root and we have no filename
+// match. Give up in this case, we don't try to find symlinks with
+// different names.
+if (ConsumedLength == 0)
+  return {};
+
 std::vector AllChildren;
 getAll(AllChildren, MatchingChild);
 StringRef Result;


Index: clang/unittests/Tooling/CompilationDatabaseTest.cpp
===
--- clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -281,6 +281,15 @@
   EXPECT_EQ("Cannot resolve relative paths", Error);
 }
 
+TEST_F(FileMatchTrieTest, SingleFile) {
+  Trie.insert("/root/RootFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+  // Add subpath to avoid `if (Children.empty())` special case
+  // which we hit at previous `find()`.
+  Trie.insert("/root/otherpath/OtherFile.cc");
+  EXPECT_EQ("", find("/root/rootfile.cc"));
+}
+
 TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
   std::string ErrorMessage;
   CompileCommand NotFound = findCompileArgsInJsonDatabase(
Index: clang/lib/Tooling/FileMatchTrie.cpp
===
--- clang/lib/Tooling/FileMatchTrie.cpp
+++ clang/lib/Tooling/FileMatchTrie.cpp
@@ -105,8 +105,13 @@
StringRef FileName,
bool &IsAmbiguous,
unsigned ConsumedLength = 0) const {
+// Note: we support only directory symlinks for performance reasons.
 if (Children.empty()) {
-  if (Comparator.equivalent(StringRef(Path), FileName))
+  // As far as we do not support file symlinks, compare
+  // basenames here to avoid request to file system.
+  if (llvm::sys::path::filename(Path) ==
+  llvm::sys::path::filename(FileName) &&
+  Comparator.equivalent(StringRef(Path), FileName))
 return StringRef(Path);
   return {};
 }
@@ -121,6 +126,13 @@
   if (!Result.empty() || IsAmbiguous)
 return Result;
 }
+
+// If `ConsumedLength` is zero, this is the root and we have no filename
+// match. Give up in this case, we don't try to find symlinks with
+// different names.
+if (ConsumedLength == 0)
+  return {};
+
 std::vector AllChildren;
 getAll(AllChildren, MatchingChild);
 StringRef Result;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D83621: [clang][Tooling] Try to avoid file system access if there is no record for the file in compile_commads.json

2020-07-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked 3 inline comments as done.
ArcsinX added a comment.

As far as I do not have commit access, could you please commit for me?
Aleksandr Platonov 




Comment at: clang/lib/Tooling/FileMatchTrie.cpp:111
+  // As far as we do not support file symlinks we compare
+  // basenames here to avoid expensive request to file system.
+  if (llvm::sys::path::filename(Path) ==

sammccall wrote:
> Here it's really just for consistency - we have a single candidate, and 
> calling equivalent() on a single file isn't expensive. I'd be happy with or 
> without this check, but the comment should mention consistency.
Removed "expensive" word.



Comment at: clang/lib/Tooling/FileMatchTrie.cpp:131
+// We failed to find the file with `Children.find()`.
+// If `ConsumedLength` is equal to 0 then we have tried to find the file
+// with it's basename. Thus, we do not have files with the same

sammccall wrote:
> nit: This is quite a lot of words to come to "we do not support file 
> symlinks" - I think it could be a bit shorter and also get into motivation:
> 
> "If `ConsumedLength` is zero, this is the root and we have no filename match. 
> Give up in this case, we don't try to find symlinks with different names".
Fixed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83621



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


[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-06-29 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
ArcsinX added a reviewer: rsmith.
ArcsinX added a project: clang.
Herald added a subscriber: cfe-commits.
ArcsinX edited the summary of this revision.

`__builtin_va_*()` and `__builtin_ms_va_*()` declared as functions with 
reference parameters.

This patch fix clang crashes at using these functions in C and passing 
incompatible structures as parameters in case of 
`__builtin_va_list`/`__builtin_ms_va_list` are structures.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82805

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-ref-c.c


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue 
reference to type '__builtin_va_list' cannot bind to a value of unrelated type 
'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4535,39 +4535,41 @@
   S.isCompleteType(Kind.getLocation(), T2)) {
 // The type we're converting from is a class type, enumerate its conversion
 // functions.
-CXXRecordDecl *T2RecordDecl = cast(T2RecordType->getDecl());
-
-const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
-for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
-  NamedDecl *D = *I;
-  CXXRecordDecl *ActingDC = cast(D->getDeclContext());
-  if (isa(D))
-D = cast(D)->getTargetDecl();
-
-  FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
-  CXXConversionDecl *Conv;
-  if (ConvTemplate)
-Conv = cast(ConvTemplate->getTemplatedDecl());
-  else
-Conv = cast(D);
-
-  // If the conversion function doesn't return a reference type,
-  // it can't be considered for this conversion unless we're allowed to
-  // consider rvalues.
-  // FIXME: Do we need to make sure that we only consider conversion
-  // candidates with reference-compatible results? That might be needed to
-  // break recursion.
-  if ((AllowRValues ||
-   Conv->getConversionType()->isLValueReferenceType())) {
+if (CXXRecordDecl *T2RecordDecl =
+dyn_cast(T2RecordType->getDecl())) {
+  const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
+  for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
+NamedDecl *D = *I;
+CXXRecordDecl *ActingDC = cast(D->getDeclContext());
+if (isa(D))
+  D = cast(D)->getTargetDecl();
+
+FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
+CXXConversionDecl *Conv;
 if (ConvTemplate)
-  S.AddTemplateConversionCandidate(
-  ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
-  CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(ConvTemplate->getTemplatedDecl());
 else
-  S.AddConversionCandidate(
-  Conv, I.getPair(), ActingDC, Initializer, DestType, CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(D);
+
+// If the conversion function doesn't return a reference type,
+// it can't be considered for this conversion unless we're allowed to
+// consider rvalues.
+// FIXME: Do we need to make sure that we only consider conversion
+// candidates with reference-compatible results? That might be needed
+// to break recursion.
+if ((AllowRValues ||
+ Conv->getConversionType()->isLValueReferenceType())) {
+  if (ConvTemplate)
+S.AddTemplateConversionCandidate(
+ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
+CandidateSet,
+/*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  else
+S.AddConversionCandidate(
+Conv, I.getPair(), ActingDC, Initializer, DestType,
+CandidateSet, /*AllowObjCConversionOnExplicit=*/false,
+AllowExplicitConvs);
+}
   }
 }
   }


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a value of unrelated type 'struct EmptyStruct'}}
+}

[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-06-29 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 274250.
ArcsinX added a comment.

Fix format


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

https://reviews.llvm.org/D82805

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-ref-c.c


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue 
reference to type '__builtin_va_list' cannot bind to a value of unrelated type 
'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4535,39 +4535,41 @@
   S.isCompleteType(Kind.getLocation(), T2)) {
 // The type we're converting from is a class type, enumerate its conversion
 // functions.
-CXXRecordDecl *T2RecordDecl = cast(T2RecordType->getDecl());
-
-const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
-for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
-  NamedDecl *D = *I;
-  CXXRecordDecl *ActingDC = cast(D->getDeclContext());
-  if (isa(D))
-D = cast(D)->getTargetDecl();
-
-  FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
-  CXXConversionDecl *Conv;
-  if (ConvTemplate)
-Conv = cast(ConvTemplate->getTemplatedDecl());
-  else
-Conv = cast(D);
-
-  // If the conversion function doesn't return a reference type,
-  // it can't be considered for this conversion unless we're allowed to
-  // consider rvalues.
-  // FIXME: Do we need to make sure that we only consider conversion
-  // candidates with reference-compatible results? That might be needed to
-  // break recursion.
-  if ((AllowRValues ||
-   Conv->getConversionType()->isLValueReferenceType())) {
+if (CXXRecordDecl *T2RecordDecl =
+dyn_cast(T2RecordType->getDecl())) {
+  const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
+  for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
+NamedDecl *D = *I;
+CXXRecordDecl *ActingDC = cast(D->getDeclContext());
+if (isa(D))
+  D = cast(D)->getTargetDecl();
+
+FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
+CXXConversionDecl *Conv;
 if (ConvTemplate)
-  S.AddTemplateConversionCandidate(
-  ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
-  CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(ConvTemplate->getTemplatedDecl());
 else
-  S.AddConversionCandidate(
-  Conv, I.getPair(), ActingDC, Initializer, DestType, CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(D);
+
+// If the conversion function doesn't return a reference type,
+// it can't be considered for this conversion unless we're allowed to
+// consider rvalues.
+// FIXME: Do we need to make sure that we only consider conversion
+// candidates with reference-compatible results? That might be needed
+// to break recursion.
+if ((AllowRValues ||
+ Conv->getConversionType()->isLValueReferenceType())) {
+  if (ConvTemplate)
+S.AddTemplateConversionCandidate(
+ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
+CandidateSet,
+/*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  else
+S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer,
+ DestType, CandidateSet,
+ /*AllowObjCConversionOnExplicit=*/false,
+ AllowExplicitConvs);
+}
   }
 }
   }


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a value of unrelated type 'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4535,39 +4535,41 @@
   S.isCompleteType(Kind.getLocation(), T2)) {
 // The ty

[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-06-29 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang/lib/Sema/SemaInit.cpp:4539
+if (CXXRecordDecl *T2RecordDecl =
+dyn_cast(T2RecordType->getDecl())) {
+  const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();

riccibruno wrote:
> I cannot reproduce this with the provided test case. Can you explain why 
> `T2RecordDecl` would not be a `CXXRecordDecl` since we should only get there 
> with c++?
You can reproduce it with
```
bin/clang -xc --target=arm-unknown-gnu -c ../clang/test/Sema/init-ref-c.c
```

We can get there with C in case of using builtins declared as functions with 
reference parameters (e.g. `__builtin_va_end()`)


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

https://reviews.llvm.org/D82805



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


[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-06-29 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 274338.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-ref-c.c


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue 
reference to type '__builtin_va_list' cannot bind to a value of unrelated type 
'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4535,39 +4535,41 @@
   S.isCompleteType(Kind.getLocation(), T2)) {
 // The type we're converting from is a class type, enumerate its conversion
 // functions.
-CXXRecordDecl *T2RecordDecl = cast(T2RecordType->getDecl());
-
-const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
-for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
-  NamedDecl *D = *I;
-  CXXRecordDecl *ActingDC = cast(D->getDeclContext());
-  if (isa(D))
-D = cast(D)->getTargetDecl();
-
-  FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
-  CXXConversionDecl *Conv;
-  if (ConvTemplate)
-Conv = cast(ConvTemplate->getTemplatedDecl());
-  else
-Conv = cast(D);
-
-  // If the conversion function doesn't return a reference type,
-  // it can't be considered for this conversion unless we're allowed to
-  // consider rvalues.
-  // FIXME: Do we need to make sure that we only consider conversion
-  // candidates with reference-compatible results? That might be needed to
-  // break recursion.
-  if ((AllowRValues ||
-   Conv->getConversionType()->isLValueReferenceType())) {
+if (CXXRecordDecl *T2RecordDecl =
+dyn_cast(T2RecordType->getDecl())) {
+  const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions();
+  for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
+NamedDecl *D = *I;
+CXXRecordDecl *ActingDC = cast(D->getDeclContext());
+if (isa(D))
+  D = cast(D)->getTargetDecl();
+
+FunctionTemplateDecl *ConvTemplate = dyn_cast(D);
+CXXConversionDecl *Conv;
 if (ConvTemplate)
-  S.AddTemplateConversionCandidate(
-  ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
-  CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(ConvTemplate->getTemplatedDecl());
 else
-  S.AddConversionCandidate(
-  Conv, I.getPair(), ActingDC, Initializer, DestType, CandidateSet,
-  /*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  Conv = cast(D);
+
+// If the conversion function doesn't return a reference type,
+// it can't be considered for this conversion unless we're allowed to
+// consider rvalues.
+// FIXME: Do we need to make sure that we only consider conversion
+// candidates with reference-compatible results? That might be needed
+// to break recursion.
+if ((AllowRValues ||
+ Conv->getConversionType()->isLValueReferenceType())) {
+  if (ConvTemplate)
+S.AddTemplateConversionCandidate(
+ConvTemplate, I.getPair(), ActingDC, Initializer, DestType,
+CandidateSet,
+/*AllowObjCConversionOnExplicit=*/false, AllowExplicitConvs);
+  else
+S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Initializer,
+ DestType, CandidateSet,
+ /*AllowObjCConversionOnExplicit=*/false,
+ AllowExplicitConvs);
+}
   }
 }
   }


Index: clang/test/Sema/init-ref-c.c
===
--- /dev/null
+++ clang/test/Sema/init-ref-c.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple arm-unknown-gnu -fsyntax-only -verify %s
+
+void f() {
+  struct EmptyStruct {};
+  struct EmptyStruct S;
+  __builtin_va_end(S); // no-crash, expected-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a value of unrelated type 'struct EmptyStruct'}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -4535,39 +4535,41 @@
   S.isCompleteType(Kind.getLocation(), T2)) {
 // The t

[PATCH] D82805: [clang] Fix a crash on passing C structure of incompatible type to function with reference parameter

2020-06-30 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked an inline comment as done.
ArcsinX added inline comments.



Comment at: clang/test/Sema/init-ref-c.c:8
+}
\ No newline at end of file


riccibruno wrote:
> I think it would be better to name this test `varargs-arm.c` since this bug 
> can only happen with the varargs-related builtins (`__builtin_addressof` 
> takes a reference but has custom type-checking).
> 
> Additional test case:
> ```
> const __builtin_va_list cva;
> __builtin_va_end(cva); // expected-error {{binding reference of type 
> '__builtin_va_list' to value of type 'const __builtin_va_list' drops 'const' 
> qualifier}}
> ```
It's not only about ARM, its about `__builtin_va_list` type which depends on 
architecture. E.g. on MIPS it also crashes.

Also it's not about `__builtin_va_*()` and `__builtin_ms_va_*()`,  its about 
builtins with reference parameters, when we pass incompatible C structures to 
these builtin calls. May be there could be other builtins with reference 
parameters in the future.

We could not pass incompatible structure to `__builtin_addressof()` that is the 
reason why we could not reproduce the problem with this builtin.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82805



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


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-04 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
ArcsinX requested review of this revision.

This patch prevents `InitListChecker::UpdateStructuredListElement()` call with 
`expr == nullptr` which could cause a crash.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85193

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-invalid-struct-array.c


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {
+  [0].i = 0,
+  [1].i = 1,
+  {  }
+};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1378,9 +1378,9 @@
 ExprResult Result = Seq.Perform(SemaRef, TmpEntity, Kind, expr);
 if (Result.isInvalid())
   hadError = true;
-
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-Result.getAs());
+else
+  UpdateStructuredListElement(StructuredList, StructuredIndex,
+  Result.getAs());
   } else if (!Seq) {
 hadError = true;
   } else if (StructuredList) {
@@ -1436,8 +1436,9 @@
 if (ExprRes.isInvalid())
   hadError = true;
   }
-  UpdateStructuredListElement(StructuredList, StructuredIndex,
-  ExprRes.getAs());
+  if (!hadError)
+UpdateStructuredListElement(StructuredList, StructuredIndex,
+ExprRes.getAs());
   ++Index;
   return;
 }


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {
+  [0].i = 0,
+  [1].i = 1,
+  {  }
+};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1378,9 +1378,9 @@
 ExprResult Result = Seq.Perform(SemaRef, TmpEntity, Kind, expr);
 if (Result.isInvalid())
   hadError = true;
-
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-Result.getAs());
+else
+  UpdateStructuredListElement(StructuredList, StructuredIndex,
+  Result.getAs());
   } else if (!Seq) {
 hadError = true;
   } else if (StructuredList) {
@@ -1436,8 +1436,9 @@
 if (ExprRes.isInvalid())
   hadError = true;
   }
-  UpdateStructuredListElement(StructuredList, StructuredIndex,
-  ExprRes.getAs());
+  if (!hadError)
+UpdateStructuredListElement(StructuredList, StructuredIndex,
+ExprRes.getAs());
   ++Index;
   return;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85301: [clang-tidy] Fix crashes in bugprone-bad-signal-to-kill-thread check.

2020-08-05 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, 
dexonsmith, steven_wu, jkorous, hiraditya, xazax.hun.
Herald added a project: clang.
ArcsinX requested review of this revision.
Herald added a subscriber: ilya-biryukov.

This patch fixes crashes in the following cases:

- `SIGTERM` is not a literal
- `SIGTERM` is a literal but `Token::PtrData == nullptr` (happens in `clangd`, 
because `clang-tidy` in `clangd` has some limitations to ensure reasonable 
performance)
- `SIGTERM` undefined after definition


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85301

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyBadSignalToKillThread) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) 
{
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {
@@ -38,6 +39,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral() || !T.getLiteralData())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 

[PATCH] D85398: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` is not a literal.

2020-08-05 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, dexonsmith, steven_wu, hiraditya, 
xazax.hun.
Herald added a project: clang.
ArcsinX requested review of this revision.

If `SIGTERM` is not a literal (e.g. `#define SIGTERM ((unsigned)15)`) 
bugprone-bad-signal-to-kill-thread check crashes.
Stack dump:

  #0 0x0217d15a llvm::sys::PrintStackTrace(llvm::raw_ostream&) 
(/llvm-project/build/bin/clang-tidy+0x217d15a)
  #1 0x0217b17c llvm::sys::RunSignalHandlers() 
(/llvm-project/build/bin/clang-tidy+0x217b17c)
  #2 0x0217b2e3 SignalHandler(int) 
(/llvm-project/build/bin/clang-tidy+0x217b2e3)
  #3 0x7f6a7efb1390 __restore_rt 
(/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
  #4 0x0212ac9b llvm::StringRef::getAsInteger(unsigned int, 
llvm::APInt&) const (/llvm-project/build/bin/clang-tidy+0x212ac9b)
  #5 0x00593501 
clang::tidy::bugprone::BadSignalToKillThreadCheck::check(clang::ast_matchers::MatchFinder::MatchResult
 const&) (/llvm-project/build/bin/clang-tidy+0x593501)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85398

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -38,6 +38,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -38,6 +38,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85401: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.

2020-08-05 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, dexonsmith, steven_wu, hiraditya, 
xazax.hun.
Herald added a project: clang.
ArcsinX requested review of this revision.

`PP->getMacroInfo()` returns nullptr for undefined macro, which leads to 
null-dereference at `MI->tockens().back()`.
Stack dump:

  #0 0x0217d15a llvm::sys::PrintStackTrace(llvm::raw_ostream&) 
(/llvm-project/build/bin/clang-tidy+0x217d15a)
  #1 0x0217b17c llvm::sys::RunSignalHandlers() 
(/llvm-project/build/bin/clang-tidy+0x217b17c)
  #2 0x0217b2e3 SignalHandler(int) 
(/llvm-project/build/bin/clang-tidy+0x217b2e3)
  #3 0x7f39be5b1390 __restore_rt 
(/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
  #4 0x00593532 
clang::tidy::bugprone::BadSignalToKillThreadCheck::check(clang::ast_matchers::MatchFinder::MatchResult
 const&) (/llvm-project/build/bin/clang-tidy+0x593532)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85401

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) 
{
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85301: [clang-tidy] Fix crashes in bugprone-bad-signal-to-kill-thread check.

2020-08-05 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85301#2197263 , @hokein wrote:

> while this patch fixes multiple issues (as described in the message), I'd 
> suggest splitting it.

Thanks for you suggestion.

> SIGTERM is not a literal

D85398 

> SIGTERM is a literal but Token::PtrData == nullptr (happens in clangd, 
> because clang-tidy in clangd has some limitations to ensure reasonable 
> performance)

For this I will create after if/when previous one will be accepted. Seems it's 
more related to clangd then clang-tidy.

> SIGTERM undefined after definition

D85401 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85301

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


[PATCH] D85401: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 283503.
ArcsinX added a comment.

Fix test according to review comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85401

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | 
count 0
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) 
{
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | count 0
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85401: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp:1
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+

hokein wrote:
> nit: `// RUN: clang-tidy %s -checks=-*,bugprone-bad-signal-to-kill-thread -- 
> | count 0`
Fixed


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85401

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


[PATCH] D85398: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` is not a literal.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 283509.
ArcsinX added a comment.

Fix test according to review comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85398

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | 
count 0
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -38,6 +38,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | count 0
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -38,6 +38,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85398: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` is not a literal.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

Thank you for review!




Comment at: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp:1
+// RUN: clang-tidy %s --checks="-*,bugprone-bad-signal-to-kill-thread"
+

hokein wrote:
> the same here.
Fixed


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85398

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


[PATCH] D85401: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was undefined after definition.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG86711668330c: [clang-tidy] Fix 
bugprone-bad-signal-to-kill-thread crash when `SIGTERM` was… (authored by 
ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85401

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | 
count 0
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) 
{
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-undef-sigterm.cpp
@@ -0,0 +1,11 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | count 0
+
+#define SIGTERM 15
+#undef SIGTERM // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -30,7 +30,8 @@
 
 void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) {
   const auto IsSigterm = [](const auto &KeyValue) -> bool {
-return KeyValue.first->getName() == "SIGTERM";
+return KeyValue.first->getName() == "SIGTERM" &&
+   KeyValue.first->hasMacroDefinition();
   };
   const auto TryExpandAsInteger =
   [](Preprocessor::macro_iterator It) -> Optional {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85398: [clang-tidy] Fix bugprone-bad-signal-to-kill-thread crash when `SIGTERM` is not a literal.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG216ad2da74f0: [clang-tidy] Fix 
bugprone-bad-signal-to-kill-thread crash when `SIGTERM` is not… (authored by 
ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85398

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | 
count 0
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,6 +39,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-bad-signal-to-kill-thread-sigterm-not-a-literal.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s --checks=-*,bugprone-bad-signal-to-kill-thread -- | count 0
+
+#define SIGTERM ((unsigned)15) // no-crash
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,6 +39,8 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
+if (!T.isLiteral())
+  return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
 llvm::APInt IntValue;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, 
dexonsmith, steven_wu, jkorous, hiraditya.
Herald added a project: clang.
ArcsinX requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

Inside clangd, clang-tidy checks don't see preprocessor events in the preamble.
This leads to `Token::PtrData == nullptr` for tokens that the macro is defined 
to.
E.g. `#define SIGTERM 15`:

- Token::Kind == tok::numeric_constant (Token::isLiteral() == true)
- Token::UintData == 2
- Token::PtrData == nullptr

As the result of this, bugprone-bad-signal-to-kill-thread check crashes at 
null-dereference inside clangd.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85417

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyBadSignalToKillThread) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyBadSignalToKillThread) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 283600.
ArcsinX added a comment.

Test rename: ClangTidyBadSignalToKillThread => 
ClangTidyNoLiteralDataInMacroToken


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp:441
 
+TEST(DiagnosticTest, ClangTidyBadSignalToKillThread) {
+  Annotations Main(R"cpp(

hokein wrote:
> ClangTidyBadSignalToKillThread doesn't seen ti provide much information about 
> what is testcase testing. Maybe `NoLiteralDataInMacroToken`?
Thanks, renamed. But I want to keep ClangTidy prefix because the problem 
related with clang-tidy limitations inside clangd.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

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


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85417#2199694 , @hokein wrote:

> Looks like NotNullTerminatedResultCheck.cpp also has this pattern, we may 
> want to fix that as well.

Yes, you are right. I will fix this in the next patch.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

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


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-06 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9f24148b2126: [clangd] Fix crash in 
bugprone-bad-signal-to-kill-thread clang-tidy check. (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

Files:
  clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -438,6 +438,21 @@
   EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
 }
 
+TEST(DiagnosticTest, ClangTidyNoLiteralDataInMacroToken) {
+  Annotations Main(R"cpp(
+#define SIGTERM 15
+using pthread_t = int;
+int pthread_kill(pthread_t thread, int sig);
+int func() {
+  pthread_t thread;
+  return pthread_kill(thread, 0);
+}
+  )cpp");
+  TestTU TU = TestTU::withCode(Main.code());
+  TU.ClangTidyChecks = "-*,bugprone-bad-signal-to-kill-thread";
+  EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
+}
+
 TEST(DiagnosticsTest, Preprocessor) {
   // This looks like a preamble, but there's an #else in the middle!
   // Check that:
Index: clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
@@ -39,7 +39,7 @@
   return llvm::None;
 const MacroInfo *MI = PP->getMacroInfo(It->first);
 const Token &T = MI->tokens().back();
-if (!T.isLiteral())
+if (!T.isLiteral() || !T.getLiteralData())
   return llvm::None;
 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85417#2199830 , @ArcsinX wrote:

> In D85417#2199694 , @hokein wrote:
>
>> Looks like NotNullTerminatedResultCheck.cpp also has this pattern, we may 
>> want to fix that as well.
>
> Yes, you are right. I will fix this in the next patch.

@hokein Sorry for bother you. Seems `bugprone-not-null-terminated-result` has 
the same 3 problems as `bugprone-bad-signal-to-kill-thread`:

- `#undef __STDC_WANT_LIB_EXT1__`
- `#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)`
- `Token::PtrData == nullptr` inside clangd

Do you expect separate 3 patches for this (as we did for 
`bugprone-bad-signal-to-kill-thread`) ?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

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


[PATCH] D85417: [clangd] Fix crash in bugprone-bad-signal-to-kill-thread clang-tidy check.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85417#2201973 , @hokein wrote:

> Thinking more about this,
>
>> Inside clangd, clang-tidy checks don't see preprocessor events in the 
>> preamble.
>> This leads to Token::PtrData == nullptr for tokens that the macro is defined 
>> to.
>> E.g. #define SIGTERM 15:
>>
>> Token::Kind == tok::numeric_constant (Token::isLiteral() == true)
>> Token::UintData == 2
>> Token::PtrData == nullptr
>
> The token is in a pretty-broken state. Do you know why the UintData is set to 
> 2, I suppose this is the length of the macro definition (`15`). If the 
> PtrData is nullptr, I'd expect the UintData is 0.

I think it's here:

  Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
 unsigned &Idx) {
Token Tok;
Tok.startToken();
Tok.setLocation(ReadSourceLocation(F, Record, Idx));
Tok.setLength(Record[Idx++]);
if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
  Tok.setIdentifierInfo(II);
Tok.setKind((tok::TokenKind)Record[Idx++]);
Tok.setFlag((Token::TokenFlags)Record[Idx++]);
return Tok;
  }

So, we set `Token::UintData` via `Token::setLength()` at preamble read, but do 
not set `Token::PtrData` without preprocessor events.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85417

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


[PATCH] D85523: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` was undefined after definition.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
ArcsinX requested review of this revision.

PP->getMacroInfo() returns nullptr for undefined macro, so we need to check 
this return value before dereference.
Stack dump:

  #0 0x02185e6a llvm::sys::PrintStackTrace(llvm::raw_ostream&) 
(/llvm-project/build/bin/clang-tidy+0x2185e6a)
  #1 0x02183e8c llvm::sys::RunSignalHandlers() 
(/llvm-project/build/bin/clang-tidy+0x2183e8c)
  #2 0x02183ff3 SignalHandler(int) 
(/llvm-project/build/bin/clang-tidy+0x2183ff3)
  #3 0x7f37df9b1390 __restore_rt 
(/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
  #4 0x0052054e 
clang::tidy::bugprone::NotNullTerminatedResultCheck::check(clang::ast_matchers::MatchFinder::MatchResult
 const&) (/llvm-project/build/bin/clang-tidy+0x52054e)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85523

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+// PP->getMacroInfo() returns nullptr if macro has no definition.
+if (MI) {
+  const auto &T = MI->tokens().back();
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = Int

[PATCH] D85525: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
ArcsinX requested review of this revision.

If `__STDC_WANT_LIB_EXT1__` is not a literal (e.g. `#define 
__STDC_WANT_LIB_EXT1__ ((unsigned)1)`) bugprone-not-null-terminated-result 
check crashes.
Stack dump:

  #0 0x02185e6a llvm::sys::PrintStackTrace(llvm::raw_ostream&) 
(/llvm-project/build/bin/clang-tidy+0x2185e6a)
  #1 0x02183e8c llvm::sys::RunSignalHandlers() 
(/llvm-project/build/bin/clang-tidy+0x2183e8c)
  #2 0x02183ff3 SignalHandler(int) 
(/llvm-project/build/bin/clang-tidy+0x2183ff3)
  #3 0x7f08d91b1390 __restore_rt 
(/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
  #4 0x021338bb llvm::StringRef::getAsInteger(unsigned int, 
llvm::APInt&) const (/llvm-project/build/bin/clang-tidy+0x21338bb)
  #5 0x0052051c 
clang::tidy::bugprone::NotNullTerminatedResultCheck::check(clang::ast_matchers::MatchFinder::MatchResult
 const&) (/llvm-project/build/bin/clang-tidy+0x52051c)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85525

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -803,10 +803,12 @@
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
 const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+if (T.isLiteral()) {
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -803,10 +803,12 @@
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
 const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+if (T.isLiteral()) {
+  StringRef ValueStr = Str

[PATCH] D85525: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: 
clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp:806
 const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+if (T.isLiteral()) {
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());

hokein wrote:
> let's add the `getLiteralData` check here --  `if (T.isLiteral() && 
> T.getLiteralData())`
Could we also add clangd test here for that case (when `T.isLiteral() == true` 
and `T.getLiteralData() == nullptr`)?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85525

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


[PATCH] D85525: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal.

2020-08-07 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c:3
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"

hokein wrote:
> I think you probably need a `// UNSUPPORTED: system-windows`, as the 
> `bugprone-not-null-terminated-result-strlen.c` does.
Seems we don't need this.  
Problem with `bugprone-not-null-terminated-result-strlen.c` test on Windows 
related with `strncmp`, according with comment.
```
// FIXME: Something wrong with the APInt un/signed conversion on Windows:
// in 'strncmp(str6, "string", 7);' it tries to inject '4294967302' as length.

// UNSUPPORTED: system-windows
```

E.g. `bugprone-not-null-terminated-result-memcpy-safe` also uses `strlen`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85525

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


[PATCH] D85523: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` was undefined after definition.

2020-08-09 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 284257.
ArcsinX added a comment.

newline


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85523

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+// PP->getMacroInfo() returns nullptr if macro has no definition.
+if (MI) {
+  const auto &T = MI->tokens().back();
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+// PP->getMacroInfo() returns nullptr if macro has no definition.
+if (MI) {
+  const auto &T = MI->tokens().back();
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85523: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` was undefined after definition.

2020-08-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5965fbf81b25: [clang-tidy] Fix a crash in 
bugprone-not-null-terminated-result check when… (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85523

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+// PP->getMacroInfo() returns nullptr if macro has no definition.
+if (MI) {
+  const auto &T = MI->tokens().back();
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-undef-stdc-want-lib-ext1.c
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ 1
+#undef __STDC_WANT_LIB_EXT1__
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -802,11 +802,14 @@
 while (It != PP->macro_end() && !AreSafeFunctionsWanted.hasValue()) {
   if (It->first->getName() == "__STDC_WANT_LIB_EXT1__") {
 const auto *MI = PP->getMacroInfo(It->first);
-const auto &T = MI->tokens().back();
-StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-llvm::APInt IntValue;
-ValueStr.getAsInteger(10, IntValue);
-AreSafeFunctionsWanted = IntValue.getZExtValue();
+// PP->getMacroInfo() returns nullptr if macro has no definition.
+if (MI) {
+  const auto &T = MI->tokens().back();
+  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+  llvm::APInt IntValue;
+  ValueStr.getAsInteger(10, IntValue);
+  AreSafeFunctionsWanted = IntValue.getZExtValue();
+}
   }
 
   ++It;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85525: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal.

2020-08-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 284296.
ArcsinX added a comment.

- Rebase
- Add T.getLiteralData() check
- Newline


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85525

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -805,10 +805,12 @@
 // PP->getMacroInfo() returns nullptr if macro has no definition.
 if (MI) {
   const auto &T = MI->tokens().back();
-  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-  llvm::APInt IntValue;
-  ValueStr.getAsInteger(10, IntValue);
-  AreSafeFunctionsWanted = IntValue.getZExtValue();
+  if (T.isLiteral() && T.getLiteralData()) {
+StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+llvm::APInt IntValue;
+ValueStr.getAsInteger(10, IntValue);
+AreSafeFunctionsWanted = IntValue.getZExtValue();
+  }
 }
   }
 


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -805,10 +805,12 @@
 // PP->getMacroInfo() returns nullptr if macro has no definition.
 if (MI) {
   const auto &T = MI->tokens().back();
-  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-  llvm::APInt IntValue;
-  ValueStr.getAsInteger(10, IntValue);
-  AreSafeFunctionsWanted = IntValue.getZExtValue();
+  if (T.isLiteral() && T.getLiteralData()) {
+StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+llvm::APInt IntValue;
+ValueStr.getAsInteger(10, IntValue);
+AreSafeFunctionsWanted = IntValue.getZExtValue();
+  }
 }
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85525: [clang-tidy] Fix a crash in bugprone-not-null-terminated-result check when `__STDC_WANT_LIB_EXT1__` is not a literal.

2020-08-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGdcb8d3b72234: [clang-tidy] Fix a crash in 
bugprone-not-null-terminated-result check when… (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85525

Files:
  clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 
'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -805,10 +805,12 @@
 // PP->getMacroInfo() returns nullptr if macro has no definition.
 if (MI) {
   const auto &T = MI->tokens().back();
-  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-  llvm::APInt IntValue;
-  ValueStr.getAsInteger(10, IntValue);
-  AreSafeFunctionsWanted = IntValue.getZExtValue();
+  if (T.isLiteral() && T.getLiteralData()) {
+StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+llvm::APInt IntValue;
+ValueStr.getAsInteger(10, IntValue);
+AreSafeFunctionsWanted = IntValue.getZExtValue();
+  }
 }
   }
 


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-not-null-terminated-result-stdc-want-lib-ext1-not-a-literal.c
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s bugprone-not-null-terminated-result %t -- \
+// RUN: -- -std=c11 -I %S/Inputs/bugprone-not-null-terminated-result
+
+#include "not-null-terminated-result-c.h"
+
+#define __STDC_LIB_EXT1__ 1
+#define __STDC_WANT_LIB_EXT1__ ((unsigned)1)
+
+void f(const char *src) {
+  char dest[13];
+  memcpy_s(dest, 13, src, strlen(src) - 1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: the result from calling 'memcpy_s' is not null-terminated [bugprone-not-null-terminated-result]
+  // CHECK-FIXES: char dest[14];
+  // CHECK-FIXES-NEXT: strncpy_s(dest, 14, src, strlen(src) - 1);
+}
+
Index: clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
===
--- clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -805,10 +805,12 @@
 // PP->getMacroInfo() returns nullptr if macro has no definition.
 if (MI) {
   const auto &T = MI->tokens().back();
-  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
-  llvm::APInt IntValue;
-  ValueStr.getAsInteger(10, IntValue);
-  AreSafeFunctionsWanted = IntValue.getZExtValue();
+  if (T.isLiteral() && T.getLiteralData()) {
+StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
+llvm::APInt IntValue;
+ValueStr.getAsInteger(10, IntValue);
+AreSafeFunctionsWanted = IntValue.getZExtValue();
+  }
 }
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-10 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85193#2204685 , @aaron.ballman 
wrote:

> Should the `StructuredIndex` still be incremented even in the case of an 
> error, as done around line 1589 and 1647?

Yes, it looks like we need to increment `StructuredIndex`.

> Do we need a similar change around line 2405?

Think so. But I could not find a test case for this.

> I sort of wonder if the correct change is to make 
> `UpdateStructuredListElement()` resilient to being passed a null `Expr *` and 
> then removing the manual increments when the expression is an error.

Should it be in this patch?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

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


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-11 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 284612.
ArcsinX added a comment.

Check for `expr == nullptr` inside 
`InitListChecker::UpdateStructuredListElement()`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-invalid-struct-array.c


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,10 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+if (expr) {
+  // This initializer overwrites a previous initializer. Warn.
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
+}
   }
 
   ++StructuredIndex;


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,10 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+if (expr) {
+  // This initializer overwrites a previous initializer. Warn.
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
+}
   }
 
   ++StructuredIndex;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-11 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85193#2207798 , @aaron.ballman 
wrote:

> In D85193#2207052 , @ArcsinX wrote:
>
>> In D85193#2204685 , @aaron.ballman 
>> wrote:
>>
>>> I sort of wonder if the correct change is to make 
>>> `UpdateStructuredListElement()` resilient to being passed a null `Expr *` 
>>> and then removing the manual increments when the expression is an error.
>>
>> Should it be in this patch?
>
> Given that we have at least four uses of this pattern in the file, I'd say it 
> should be this patch. WDYT?

Agree, updated patch.

But I am not sure about correct place for `expr` check. Seems `nullptr` 
dereference possible only here: `diagnoseInitOverride(PrevInit, 
expr->getSourceRange());`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

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


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-11 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: clang/lib/Sema/SemaInit.cpp:3088
   // No structured initializer list to update
   if (!StructuredList)
 return;

aaron.ballman wrote:
> I would move the check up to here as the only time we should get a null 
> expression is if something else has gone wrong (and update the comment as 
> well).
Could we add `expr` check after `return`? As far as we need to increment 
`StructuredIndex` in that case.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

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


[PATCH] D85193: [clang] Do not use an invalid expression to update the initializer.

2020-08-12 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 284987.
ArcsinX added a comment.

Update comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-invalid-struct-array.c


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,10 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// Emit warning if `expr` is valid.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,10 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// Emit warning if `expr` is valid.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85193: [clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`

2020-08-12 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 285101.
ArcsinX added a comment.

Fix comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-invalid-struct-array.c


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,12 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// No need to diagnose when `expr` is nullptr because a more relevant
+// diagnostic has already been issued and this diagnostic is potentially
+// noise.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,12 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// No need to diagnose when `expr` is nullptr because a more relevant
+// diagnostic has already been issued and this diagnostic is potentially
+// noise.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85193: [clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`

2020-08-12 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

In D85193#2212976 , @aaron.ballman 
wrote:

> I have a suggestion for the added comment,

Fixed comment according to your suggestion.

> but I also forgot to ask, do you need someone to commit on your behalf?

I already have commit access.

Thanks for review!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

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


[PATCH] D85193: [clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`

2020-08-12 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3fa0a039ab6f: [clang] Check `expr` inside 
`InitListChecker::UpdateStructuredListElement()` (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85193

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-invalid-struct-array.c


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,12 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// No need to diagnose when `expr` is nullptr because a more relevant
+// diagnostic has already been issued and this diagnostic is potentially
+// noise.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;


Index: clang/test/Sema/init-invalid-struct-array.c
===
--- /dev/null
+++ clang/test/Sema/init-invalid-struct-array.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+struct S {
+  Unknown u; // expected-error {{unknown type name 'Unknown'}}
+  int i;
+};
+// Should not crash
+struct S s[] = {[0].i = 0, [1].i = 1, {}};
Index: clang/lib/Sema/SemaInit.cpp
===
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -1585,10 +1585,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
 }
 
@@ -1643,10 +1640,7 @@
   if (!VerifyOnly && expr)
 IList->setInit(Index, expr);
 
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
   ++Index;
 }
 
@@ -1697,11 +1691,7 @@
   IList->setInit(Index, ResultExpr);
 }
   }
-  if (hadError)
-++StructuredIndex;
-  else
-UpdateStructuredListElement(StructuredList, StructuredIndex,
-ResultExpr);
+  UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
   ++Index;
   return;
 }
@@ -3100,8 +3090,12 @@
 
   if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
   StructuredIndex, expr)) {
-// This initializer overwrites a previous initializer. Warn.
-diagnoseInitOverride(PrevInit, expr->getSourceRange());
+// This initializer overwrites a previous initializer.
+// No need to diagnose when `expr` is nullptr because a more relevant
+// diagnostic has already been issued and this diagnostic is potentially
+// noise.
+if (expr)
+  diagnoseInitOverride(PrevInit, expr->getSourceRange());
   }
 
   ++StructuredIndex;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133043: [clangd] Fix tests for implicit C function declaration

2022-08-31 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
ArcsinX added reviewers: aaron.ballman, sammccall, kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
ArcsinX requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

clangd code fixes at D122983  were not right.
We need to check that clangd provides IncludeFixer fixits for implicit function 
declaration even if this is not an error (e.g. implicit function declaration in 
C89).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133043

Files:
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1465,11 +1465,12 @@
 TEST(IncludeFixerTest, HeaderNamedInDiag) {
   Annotations Test(R"cpp(
 $insert[[]]int main() {
-  [[printf]](""); // error-ok
+  [[printf]]("");
 }
   )cpp");
   auto TU = TestTU::withCode(Test.code());
-  TU.ExtraArgs = {"-xc"};
+  TU.ExtraArgs = {"-xc", "-std=c99",
+  "-Wno-error=implicit-function-declaration"};
   auto Index = buildIndexWithSymbol({});
   TU.ExternalIndex = Index.get();
 
@@ -1482,13 +1483,22 @@
  "declarations"),
   withFix(Fix(Test.range("insert"), "#include \n",
   "Include  for symbol printf");
+
+  TU.ExtraArgs = {"-xc", "-std=c89"};
+  EXPECT_THAT(
+  *TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicitly declaring library function 'printf' "
+ "with type 'int (const char *, ...)'"),
+  withFix(Fix(Test.range("insert"), "#include \n",
+  "Include  for symbol printf");
 }
 
 TEST(IncludeFixerTest, CImplicitFunctionDecl) {
-  Annotations Test("void x() { [[foo]](); /* error-ok */ }");
+  Annotations Test("void x() { [[foo]](); }");
   auto TU = TestTU::withCode(Test.code());
   TU.Filename = "test.c";
-  TU.ExtraArgs.push_back("-std=c99");
+  TU.ExtraArgs = {"-std=c99", "-Wno-error=implicit-function-declaration"};
 
   Symbol Sym = func("foo");
   Sym.Flags |= Symbol::IndexedForCodeCompletion;
@@ -1509,6 +1519,13 @@
"support implicit function declarations"),
   withFix(Fix(Range{}, "#include \"foo.h\"\n",
   "Include \"foo.h\" for symbol foo");
+
+  TU.ExtraArgs = {"-std=c89", "-Wall"};
+  EXPECT_THAT(*TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicit declaration of function 'foo'"),
+  withFix(Fix(Range{}, "#include \"foo.h\"\n",
+  "Include \"foo.h\" for symbol foo");
 }
 
 TEST(DiagsInHeaders, DiagInsideHeader) {
Index: clang-tools-extra/clangd/ParsedAST.cpp
===
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -493,6 +493,7 @@
 // We restore the original severity in the level adjuster.
 // FIXME: It would be better to have a real API for this, but what?
 for (auto ID : {diag::ext_implicit_function_decl_c99,
+diag::ext_implicit_lib_function_decl,
 diag::ext_implicit_lib_function_decl_c99,
 diag::warn_implicit_function_decl}) {
   OverriddenSeverity.try_emplace(


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1465,11 +1465,12 @@
 TEST(IncludeFixerTest, HeaderNamedInDiag) {
   Annotations Test(R"cpp(
 $insert[[]]int main() {
-  [[printf]](""); // error-ok
+  [[printf]]("");
 }
   )cpp");
   auto TU = TestTU::withCode(Test.code());
-  TU.ExtraArgs = {"-xc"};
+  TU.ExtraArgs = {"-xc", "-std=c99",
+  "-Wno-error=implicit-function-declaration"};
   auto Index = buildIndexWithSymbol({});
   TU.ExternalIndex = Index.get();
 
@@ -1482,13 +1483,22 @@
  "declarations"),
   withFix(Fix(Test.range("insert"), "#include \n",
   "Include  for symbol printf");
+
+  TU.ExtraArgs = {"-xc", "-std=c89"};
+  EXPECT_THAT(
+  *TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicitly declaring library function 'printf' "
+ "with type 'int (const char *, ...)'"),
+  withFix(Fix(Test.range("insert"), "#include \n",
+  "Include  for symbol printf");
 }
 
 TEST(IncludeFixerTest, CImplicitFu

[PATCH] D133043: [clangd] Fix tests for implicit C function declaration

2022-09-01 Thread Aleksandr Platonov via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcc4b86cfc01c: [clangd] Fix tests for implicit C function 
declaration (authored by ArcsinX).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133043

Files:
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1465,11 +1465,12 @@
 TEST(IncludeFixerTest, HeaderNamedInDiag) {
   Annotations Test(R"cpp(
 $insert[[]]int main() {
-  [[printf]](""); // error-ok
+  [[printf]]("");
 }
   )cpp");
   auto TU = TestTU::withCode(Test.code());
-  TU.ExtraArgs = {"-xc"};
+  TU.ExtraArgs = {"-xc", "-std=c99",
+  "-Wno-error=implicit-function-declaration"};
   auto Index = buildIndexWithSymbol({});
   TU.ExternalIndex = Index.get();
 
@@ -1482,13 +1483,22 @@
  "declarations"),
   withFix(Fix(Test.range("insert"), "#include \n",
   "Include  for symbol printf");
+
+  TU.ExtraArgs = {"-xc", "-std=c89"};
+  EXPECT_THAT(
+  *TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicitly declaring library function 'printf' "
+ "with type 'int (const char *, ...)'"),
+  withFix(Fix(Test.range("insert"), "#include \n",
+  "Include  for symbol printf");
 }
 
 TEST(IncludeFixerTest, CImplicitFunctionDecl) {
-  Annotations Test("void x() { [[foo]](); /* error-ok */ }");
+  Annotations Test("void x() { [[foo]](); }");
   auto TU = TestTU::withCode(Test.code());
   TU.Filename = "test.c";
-  TU.ExtraArgs.push_back("-std=c99");
+  TU.ExtraArgs = {"-std=c99", "-Wno-error=implicit-function-declaration"};
 
   Symbol Sym = func("foo");
   Sym.Flags |= Symbol::IndexedForCodeCompletion;
@@ -1509,6 +1519,13 @@
"support implicit function declarations"),
   withFix(Fix(Range{}, "#include \"foo.h\"\n",
   "Include \"foo.h\" for symbol foo");
+
+  TU.ExtraArgs = {"-std=c89", "-Wall"};
+  EXPECT_THAT(*TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicit declaration of function 'foo'"),
+  withFix(Fix(Range{}, "#include \"foo.h\"\n",
+  "Include \"foo.h\" for symbol foo");
 }
 
 TEST(DiagsInHeaders, DiagInsideHeader) {
Index: clang-tools-extra/clangd/ParsedAST.cpp
===
--- clang-tools-extra/clangd/ParsedAST.cpp
+++ clang-tools-extra/clangd/ParsedAST.cpp
@@ -493,6 +493,7 @@
 // We restore the original severity in the level adjuster.
 // FIXME: It would be better to have a real API for this, but what?
 for (auto ID : {diag::ext_implicit_function_decl_c99,
+diag::ext_implicit_lib_function_decl,
 diag::ext_implicit_lib_function_decl_c99,
 diag::warn_implicit_function_decl}) {
   OverriddenSeverity.try_emplace(


Index: clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
===
--- clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -1465,11 +1465,12 @@
 TEST(IncludeFixerTest, HeaderNamedInDiag) {
   Annotations Test(R"cpp(
 $insert[[]]int main() {
-  [[printf]](""); // error-ok
+  [[printf]]("");
 }
   )cpp");
   auto TU = TestTU::withCode(Test.code());
-  TU.ExtraArgs = {"-xc"};
+  TU.ExtraArgs = {"-xc", "-std=c99",
+  "-Wno-error=implicit-function-declaration"};
   auto Index = buildIndexWithSymbol({});
   TU.ExternalIndex = Index.get();
 
@@ -1482,13 +1483,22 @@
  "declarations"),
   withFix(Fix(Test.range("insert"), "#include \n",
   "Include  for symbol printf");
+
+  TU.ExtraArgs = {"-xc", "-std=c89"};
+  EXPECT_THAT(
+  *TU.build().getDiagnostics(),
+  ElementsAre(AllOf(
+  Diag(Test.range(), "implicitly declaring library function 'printf' "
+ "with type 'int (const char *, ...)'"),
+  withFix(Fix(Test.range("insert"), "#include \n",
+  "Include  for symbol printf");
 }
 
 TEST(IncludeFixerTest, CImplicitFunctionDecl) {
-  Annotations Test("void x() { [[foo]](); /* error-ok */ }");
+  Annotations Test("void x() { [[foo]](); }");
   auto TU = TestTU::withCode(Test.code());
   TU.Filename = "test.c";
-  TU.ExtraArgs.push_back("-std=c99");
+  TU.ExtraArgs = {"-std=c99", "-Wno-error=implicit-function-declarat

[PATCH] D133886: [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors

2022-09-14 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX created this revision.
ArcsinX added reviewers: hokein, kadircet.
Herald added a project: All.
ArcsinX requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch fixes a crash which appears because of CheckArgAlignment() call with 
error type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133886

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/recovery-expr-type.cpp


Index: clang/test/SemaCXX/recovery-expr-type.cpp
===
--- clang/test/SemaCXX/recovery-expr-type.cpp
+++ clang/test/SemaCXX/recovery-expr-type.cpp
@@ -162,3 +162,15 @@
   b = __builtin_object_size(c, 0); // crash2
 }
 }
+
+namespace test15 {
+const UnknownType SZ = 8; // expected-error {{unknown type}}
+typedef unsigned char UC;
+void f() {
+  struct {
+void m(UC (&)[SZ]) {}
+  } S;
+  unsigned char A[8];
+  S.m(A); // no crash
+}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -5776,6 +5776,8 @@
   checkAIXMemberAlignment((Arg->getExprLoc()), Arg);
 
 QualType ParamTy = Proto->getParamType(ArgIdx);
+if (ParamTy->containsErrors())
+  continue;
 QualType ArgTy = Arg->getType();
 CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1),
   ArgTy, ParamTy);


Index: clang/test/SemaCXX/recovery-expr-type.cpp
===
--- clang/test/SemaCXX/recovery-expr-type.cpp
+++ clang/test/SemaCXX/recovery-expr-type.cpp
@@ -162,3 +162,15 @@
   b = __builtin_object_size(c, 0); // crash2
 }
 }
+
+namespace test15 {
+const UnknownType SZ = 8; // expected-error {{unknown type}}
+typedef unsigned char UC;
+void f() {
+  struct {
+void m(UC (&)[SZ]) {}
+  } S;
+  unsigned char A[8];
+  S.m(A); // no crash
+}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -5776,6 +5776,8 @@
   checkAIXMemberAlignment((Arg->getExprLoc()), Arg);
 
 QualType ParamTy = Proto->getParamType(ArgIdx);
+if (ParamTy->containsErrors())
+  continue;
 QualType ArgTy = Arg->getType();
 CheckArgAlignment(Arg->getExprLoc(), FDecl, std::to_string(ArgIdx + 1),
   ArgTy, ParamTy);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133886: [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors

2022-09-14 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added a comment.

This looks similar to the problem fixed in D103825 



Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133886

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


[PATCH] D133886: [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors

2022-09-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX added inline comments.



Comment at: clang/lib/Sema/SemaChecking.cpp:5779
 QualType ParamTy = Proto->getParamType(ArgIdx);
+if (ParamTy->containsErrors())
+  continue;

hokein wrote:
> It looks like for the failure case the `ParamTy` for the parameter is a 
> dependent array type, and it violates the "non-dependent" assumption of 
> `clang::ASTContext::getTypeInfoImpl` which is called by  
> `getTypeAlignInChars` in `CheckArgAlignment`.
> 
> so I'd suggest moving the fix to `CheckArgAlignment` line 5685 (adding a 
> `ParamTy->isDependentType()` to the `if` condition).
When I found this problem I fixed it like you are suggesting. But after that I 
replaced it with this check, because it seems there is no reason to try to 
check something on code with errors.

I mean that even if we are not crashing, results from `CheckArgAlignment()` 
can't be trusted if we are passing  something with errors to it.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133886

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


[PATCH] D133886: [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors

2022-09-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX updated this revision to Diff 460358.
ArcsinX added a comment.

- Check for dependent type inside CheckArgAlignment()
- Simplify test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133886

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/recovery-expr-type.cpp


Index: clang/test/SemaCXX/recovery-expr-type.cpp
===
--- clang/test/SemaCXX/recovery-expr-type.cpp
+++ clang/test/SemaCXX/recovery-expr-type.cpp
@@ -162,3 +162,12 @@
   b = __builtin_object_size(c, 0); // crash2
 }
 }
+
+namespace test15 {
+void f() {
+  struct {
+void m(int (&)[undefined()]) {} // expected-error {{undeclared identifier}}
+  } S;
+  S.m(1); // no crash
+}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -5690,9 +5690,9 @@
 
   // Find expected alignment, and the actual alignment of the passed object.
   // getTypeAlignInChars requires complete types
-  if (ArgTy.isNull() || ParamTy->isIncompleteType() ||
-  ArgTy->isIncompleteType() || ParamTy->isUndeducedType() ||
-  ArgTy->isUndeducedType())
+  if (ArgTy.isNull() || ParamTy->isDependentType() ||
+  ParamTy->isIncompleteType() || ArgTy->isIncompleteType() ||
+  ParamTy->isUndeducedType() || ArgTy->isUndeducedType())
 return;
 
   CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy);


Index: clang/test/SemaCXX/recovery-expr-type.cpp
===
--- clang/test/SemaCXX/recovery-expr-type.cpp
+++ clang/test/SemaCXX/recovery-expr-type.cpp
@@ -162,3 +162,12 @@
   b = __builtin_object_size(c, 0); // crash2
 }
 }
+
+namespace test15 {
+void f() {
+  struct {
+void m(int (&)[undefined()]) {} // expected-error {{undeclared identifier}}
+  } S;
+  S.m(1); // no crash
+}
+}
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -5690,9 +5690,9 @@
 
   // Find expected alignment, and the actual alignment of the passed object.
   // getTypeAlignInChars requires complete types
-  if (ArgTy.isNull() || ParamTy->isIncompleteType() ||
-  ArgTy->isIncompleteType() || ParamTy->isUndeducedType() ||
-  ArgTy->isUndeducedType())
+  if (ArgTy.isNull() || ParamTy->isDependentType() ||
+  ParamTy->isIncompleteType() || ArgTy->isIncompleteType() ||
+  ParamTy->isUndeducedType() || ArgTy->isUndeducedType())
 return;
 
   CharUnits ParamAlign = Context.getTypeAlignInChars(ParamTy);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133886: [clang][RecoveryExpr] Don't perform alignment check if parameter type contains errors

2022-09-15 Thread Aleksandr Platonov via Phabricator via cfe-commits
ArcsinX marked 3 inline comments as done.
ArcsinX added inline comments.



Comment at: clang/lib/Sema/SemaChecking.cpp:5779
 QualType ParamTy = Proto->getParamType(ArgIdx);
+if (ParamTy->containsErrors())
+  continue;

hokein wrote:
> ArcsinX wrote:
> > hokein wrote:
> > > It looks like for the failure case the `ParamTy` for the parameter is a 
> > > dependent array type, and it violates the "non-dependent" assumption of 
> > > `clang::ASTContext::getTypeInfoImpl` which is called by  
> > > `getTypeAlignInChars` in `CheckArgAlignment`.
> > > 
> > > so I'd suggest moving the fix to `CheckArgAlignment` line 5685 (adding a 
> > > `ParamTy->isDependentType()` to the `if` condition).
> > When I found this problem I fixed it like you are suggesting. But after 
> > that I replaced it with this check, because it seems there is no reason to 
> > try to check something on code with errors.
> > 
> > I mean that even if we are not crashing, results from `CheckArgAlignment()` 
> > can't be trusted if we are passing  something with errors to it.
> > because it seems there is no reason to try to check something on code with 
> > errors.
> 
> I think this is case-by-case (for this case, it may be true) -- in some cases 
> (see test7 and test8 for example in `recovery-expr-type.cpp`), we do want to 
> check something even on the code with errors to emit useful secondary 
> diagnostics. In general we want to emit a full list of diagnostics and at the 
> same time avoid any suspicious diagnostics, however achieving both goals is 
> hard.
> 
> Depending on how fatal the error is
> 
> - if the error is fatal, RecoveryError is just a dependent-type wrapper, we 
> should not call check* to emit diagnostics (we're less certain about the 
> quality of these diagnostics), this is mostly done by leveraging on the 
> existing template dependent mechanism;
> - if the error is minor, RecoveryError preserves a concrete type, we might 
> want call check* to emit diagnostics as we're more confident; 
> 
> The current solution makes sense to fix the crash, but I think the main 
> reason is that `CheckArgAlignment` now can be invoked with a dependent-type 
> parameter in non-template context (after we extended the "dependent" concept 
> from depending on template parameters to depending on template parameters and 
> errors), so we should fix `CheckArgAlignment`.
> 
Thanks for clarification. Moved check inside CheckArgAlignment() as you 
suggested.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133886

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


  1   2   3   >