[clang-tools-extra] e07753c - [clang-tidy] Fix test that requires Windows platofrm

2021-05-13 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-05-13T15:51:53+03:00
New Revision: e07753c8814dba100dcae44e2b47947b340ad0e8

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

LOG: [clang-tidy] Fix test that requires Windows platofrm

This commit fixes the cppcoreguidelines-pro-type-vararg test when it
runs on a Windows host, but the toolchain is targeted a non-Windows
platform.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D102337

Added: 


Modified: 

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp

Removed: 




diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
index 9cb82497a3ae..2a4051599f54 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
@@ -2,9 +2,7 @@
 // Ensure that the 'cppcoreguidelines-pro-type-vararg' check works with the
 // built-in va_list on Windows systems.
 
-// REQUIRES: system-windows
-
-// RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-vararg %t
+// RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-vararg %t -- 
--extra-arg=--target=x86_64-windows
 
 void test_ms_va_list(int a, ...) {
   __builtin_ms_va_list ap;



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


[clang-tools-extra] ab92a4c - [clang-tidy] Fix altera-struct-pack-align crash for struct fields with incomplete type

2021-05-17 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-05-17T16:50:47+03:00
New Revision: ab92a4c26f54170bf72706ad29c0fb151a177590

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

LOG: [clang-tidy] Fix altera-struct-pack-align crash for struct fields with 
incomplete type

We can only use ASTContext::getTypeInfo for complete types.

This fixes bugzilla issue 50313.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D102569

Added: 

clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp

Modified: 
clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp 
b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
index a2178befa9df..ef5fe41fd8c9 100644
--- a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
@@ -58,9 +58,11 @@ void StructPackAlignCheck::check(const 
MatchFinder::MatchResult &Result) {
 // For each StructField, record how big it is (in bits).
 // Would be good to use a pair of  to advise a better
 // packing order.
+QualType StructFieldTy = StructField->getType();
+if (StructFieldTy->isIncompleteType())
+  return;
 unsigned int StructFieldWidth =
-(unsigned int)Result.Context
-->getTypeInfo(StructField->getType().getTypePtr())
+(unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
 .Width;
 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
 // FIXME: Recommend a reorganization of the struct (sort by StructField

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp
new file mode 100644
index ..660addcbe803
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy -expect-clang-tidy-error %s altera-struct-pack-align 
%t -- -header-filter=.*
+
+struct A;
+struct B {
+  A a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: error: field has incomplete type 'A' 
[clang-diagnostic-error]
+};



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


[clang] c558b1f - [analyzer] Fix calculating offset for fields with an empty type

2021-07-03 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-07-04T06:57:11+03:00
New Revision: c558b1fca7350f4f4d8e7387fb2ead951284a5cf

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

LOG: [analyzer] Fix calculating offset for fields with an empty type

Fix offset calculation routines in padding checker to avoid assertion
errors described in bugzilla issue 50426. The fields that are subojbects
of zero size, marked with [[no_unique_address]] or empty bitfields will
be excluded from padding calculation routines.

Reviewed By: NoQ

Differential Revision: https://reviews.llvm.org/D104097

Added: 
clang/test/Analysis/padding_no_unique_address.cpp

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

Removed: 




diff  --git a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
index 96f0d9bb3c3de..40472ccfe7e66 100644
--- a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
@@ -193,6 +193,11 @@ class PaddingChecker : public 
Checker> {
 CharUnits PaddingSum;
 CharUnits Offset = ASTContext.toCharUnitsFromBits(RL.getFieldOffset(0));
 for (const FieldDecl *FD : RD->fields()) {
+  // Skip field that is a subobject of zero size, marked with
+  // [[no_unique_address]] or an empty bitfield, because its address can be
+  // set the same as the other fields addresses.
+  if (FD->isZeroSize(ASTContext))
+continue;
   // This checker only cares about the padded size of the
   // field, and not the data size. If the field is a record
   // with tail padding, then we won't put that number in our
@@ -249,7 +254,7 @@ class PaddingChecker : public 
Checker> {
   RetVal.Field = FD;
   auto &Ctx = FD->getASTContext();
   auto Info = Ctx.getTypeInfoInChars(FD->getType());
-  RetVal.Size = Info.Width;
+  RetVal.Size = FD->isZeroSize(Ctx) ? CharUnits::Zero() : Info.Width;
   RetVal.Align = Info.Align;
   assert(llvm::isPowerOf2_64(RetVal.Align.getQuantity()));
   if (auto Max = FD->getMaxAlignment())

diff  --git a/clang/test/Analysis/padding_no_unique_address.cpp 
b/clang/test/Analysis/padding_no_unique_address.cpp
new file mode 100644
index 0..4f26922c9450d
--- /dev/null
+++ b/clang/test/Analysis/padding_no_unique_address.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-linux-gnu 
-analyzer-checker=optin.performance -analyzer-config 
optin.performance.Padding:AllowedPad=2 -verify %s
+
+class Empty {}; // no-warning
+
+// expected-warning@+1{{Excessive padding in 'struct NoUniqueAddressWarn1' (6 
padding}}
+struct NoUniqueAddressWarn1 {
+  char c1;
+  [[no_unique_address]] Empty empty;
+  int i;
+  char c2;
+};
+
+// expected-warning@+1{{Excessive padding in 'struct NoUniqueAddressWarn2' (6 
padding}}
+struct NoUniqueAddressWarn2 {
+char c1;
+[[no_unique_address]] Empty e1, e2;
+int i;
+char c2;
+};
+
+struct NoUniqueAddressNoWarn1 {
+  char c1;
+  [[no_unique_address]] Empty empty;
+  char c2;
+};
+
+struct NoUniqueAddressNoWarn2 {
+  char c1;
+  [[no_unique_address]] Empty e1, e2;
+};



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


[clang] 3697f26 - [docs] Fix linking issues in LibASTMatchers tutorial

2021-07-05 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-07-05T12:11:25+03:00
New Revision: 3697f2683695a5e67184c4b348415f4da028133c

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

LOG: [docs] Fix linking issues in LibASTMatchers tutorial

Update CMakeLists.txt in the tutorial to reflect the latest changes in
LLVM. The demo project cannot be linked without added libraries.

Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D105409

Added: 


Modified: 
clang/docs/LibASTMatchersTutorial.rst

Removed: 




diff  --git a/clang/docs/LibASTMatchersTutorial.rst 
b/clang/docs/LibASTMatchersTutorial.rst
index f70173e9f83c9..3f396dd39ded1 100644
--- a/clang/docs/LibASTMatchersTutorial.rst
+++ b/clang/docs/LibASTMatchersTutorial.rst
@@ -105,9 +105,12 @@ CMakeLists.txt should have the following contents:
 )
   target_link_libraries(loop-convert
 PRIVATE
-clangTooling
-clangBasic
+clangAST
 clangASTMatchers
+clangBasic
+clangFrontend
+clangSerialization
+clangTooling
 )
 
 With that done, Ninja will be able to compile our tool. Let's give it



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


[clang-tools-extra] c2e9baf - [clang-tidy] Fix cppcoreguidelines-pro-type-vararg false positives with __builtin_ms_va_list

2021-05-04 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-05-04T13:49:20+03:00
New Revision: c2e9baf2e8dafe92f57fe4171d4b6a5f50d5999e

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

LOG: [clang-tidy] Fix cppcoreguidelines-pro-type-vararg false positives with 
__builtin_ms_va_list

This commit fixes cppcoreguidelines-pro-type-vararg false positives on
'char *' variables.

The incorrect warnings generated by clang-tidy can be illustrated with
the following minimal example:

```
goid foo(char* in) {
  char *tmp = in;
}
```

The problem is that __builtin_ms_va_list desugared as 'char *', which
leads to false positives.

Fixes bugzilla issue 48042.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D101259

Added: 

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp

Modified: 
clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
index ec0e87ae22c05..b26cd59fd672a 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp
@@ -10,6 +10,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/TargetInfo.h"
 
 using namespace clang::ast_matchers;
 
@@ -60,8 +61,45 @@ namespace {
 AST_MATCHER(QualType, isVAList) {
   ASTContext &Context = Finder->getASTContext();
   QualType Desugar = Node.getDesugaredType(Context);
-  return Context.getBuiltinVaListType().getDesugaredType(Context) == Desugar ||
- Context.getBuiltinMSVaListType().getDesugaredType(Context) == Desugar;
+  QualType NodeTy = Node.getUnqualifiedType();
+
+  auto CheckVaList = [](QualType NodeTy, QualType Expected,
+const ASTContext &Context) {
+if (NodeTy == Expected)
+  return true;
+QualType Desugar = NodeTy;
+QualType Ty;
+do {
+  Ty = Desugar;
+  Desugar = Ty.getSingleStepDesugaredType(Context);
+  if (Desugar == Expected)
+return true;
+} while (Desugar != Ty);
+return false;
+  };
+
+  // The internal implementation of __builtin_va_list depends on the target
+  // type. Some targets implements va_list as 'char *' or 'void *'.
+  // In these cases we need to remove all typedefs one by one to check this.
+  using BuiltinVaListKind = TargetInfo::BuiltinVaListKind;
+  BuiltinVaListKind VaListKind = 
Context.getTargetInfo().getBuiltinVaListKind();
+  if (VaListKind == BuiltinVaListKind::CharPtrBuiltinVaList ||
+  VaListKind == BuiltinVaListKind::VoidPtrBuiltinVaList) {
+if (CheckVaList(NodeTy, Context.getBuiltinVaListType(), Context))
+  return true;
+  } else if (Desugar ==
+ Context.getBuiltinVaListType().getDesugaredType(Context)) {
+return true;
+  }
+
+  // We also need to check the implementation of __builtin_ms_va_list in the
+  // same way, because it may 
diff er from the va_list implementation.
+  if (Desugar == Context.getBuiltinMSVaListType().getDesugaredType(Context) &&
+  CheckVaList(NodeTy, Context.getBuiltinMSVaListType(), Context)) {
+return true;
+  }
+
+  return false;
 }
 
 AST_MATCHER_P(AdjustedType, hasOriginalType,

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
new file mode 100644
index 0..9cb82497a3aec
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
@@ -0,0 +1,26 @@
+// Purpose:
+// Ensure that the 'cppcoreguidelines-pro-type-vararg' check works with the
+// built-in va_list on Windows systems.
+
+// REQUIRES: system-windows
+
+// RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-vararg %t
+
+void test_ms_va_list(int a, ...) {
+  __builtin_ms_va_list ap;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare variables of type 
va_list; use variadic templates instead
+  __builtin_ms_va_start(ap, a);
+  int b = __builtin_va_arg(ap, int);
+  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use va_arg to define 
c-style vararg functions; use variadic templates instead
+  __builtin_ms_va_end(ap);
+}
+
+void test_typedefs(int a, ...) {
+  typedef __builtin_ms_va_list my_va_list1;
+  my_va_list1 ap1;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare variables of type 
va_list; use variadic templates instead
+
+  using my_va_list2 = __builtin_ms_va_list;
+  my_va

[clang-tools-extra] 9a930aa - [clang-tidy] Avoid bugprone-macro-parentheses warnings after goto argument

2021-04-22 Thread Georgy Komarov via cfe-commits

Author: Georgy Komarov
Date: 2021-04-22T10:14:10+03:00
New Revision: 9a930aa5bd2fc4686002d02411141a19f0ad8f36

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

LOG: [clang-tidy] Avoid bugprone-macro-parentheses warnings after goto argument

clang-tidy should not generate warnings for the goto argument without
parentheses, because it would be a syntax error.

The only valid case where an argument can be enclosed in parentheses is
"Labels as Values" gcc extension: 
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html.
This commit adds support for the label-as-values extension as implemented in 
clang.

Fixes bugzilla issue 49634.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D99924

Added: 


Modified: 
clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
index 8d4366b51a3ec..303119d8ec812 100644
--- a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
@@ -158,6 +158,9 @@ void MacroParenthesesPPCallbacks::argument(const Token 
&MacroNameTok,
   // Skip variable declaration.
   bool VarDecl = possibleVarDecl(MI, MI->tokens_begin());
 
+  // Skip the goto argument with an arbitrary number of subsequent stars.
+  bool FoundGoto = false;
+
   for (auto TI = MI->tokens_begin(), TE = MI->tokens_end(); TI != TE; ++TI) {
 // First token.
 if (TI == MI->tokens_begin())
@@ -179,9 +182,17 @@ void MacroParenthesesPPCallbacks::argument(const Token 
&MacroNameTok,
   continue;
 }
 
+// There should not be extra parentheses for the goto argument.
+if (Tok.is(tok::kw_goto)) {
+  FoundGoto = true;
+  continue;
+}
+
 // Only interested in identifiers.
-if (!Tok.isOneOf(tok::identifier, tok::raw_identifier))
+if (!Tok.isOneOf(tok::identifier, tok::raw_identifier)) {
+  FoundGoto = false;
   continue;
+}
 
 // Only interested in macro arguments.
 if (MI->getParameterNum(Tok.getIdentifierInfo()) < 0)
@@ -239,12 +250,16 @@ void MacroParenthesesPPCallbacks::argument(const Token 
&MacroNameTok,
 if (MI->isVariadic())
   continue;
 
-Check->diag(Tok.getLocation(), "macro argument should be enclosed in "
-   "parentheses")
-<< FixItHint::CreateInsertion(Tok.getLocation(), "(")
-<< FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset(
-  PP->getSpelling(Tok).length()),
-  ")");
+if (!FoundGoto) {
+  Check->diag(Tok.getLocation(), "macro argument should be enclosed in "
+ "parentheses")
+  << FixItHint::CreateInsertion(Tok.getLocation(), "(")
+  << FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset(
+PP->getSpelling(Tok).length()),
+")");
+}
+
+FoundGoto = false;
   }
 }
 

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp
index 8d128352e7894..6c2f42dd2dcd6 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp
@@ -10,6 +10,10 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: macro argument should be enclosed 
in parentheses [bugprone-macro-parentheses]
 #define BAD5(X)   A*B=(C*)X+2
 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: macro argument should be enclosed 
in parentheses [bugprone-macro-parentheses]
+#define BAD6(x)   goto *x;
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: macro argument should be enclosed 
in parentheses [bugprone-macro-parentheses]
+#define BAD7(x, y)if (x) goto y; else x;
+// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: macro argument should be enclosed 
in parentheses [bugprone-macro-parentheses]
 
 #define GOOD1 1
 #define GOOD2 (1+2)
@@ -44,6 +48,8 @@
 #define GOOD31(X) A*X=2
 #define GOOD32(X) std::vector
 #define GOOD33(x) if (!a__##x) a_##x = &f(#x)
+#define GOOD34(x, y)  if (x) goto y;
+#define GOOD35(x, y)  if (x) goto *(y);
 
 // These are allowed for now..
 #define MAYBE1*12.34



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