[PATCH] D19586: Misleading Indentation check
Pajesz requested a review of this revision. Pajesz added reviewers: dkrupp, xazax.hun, nlewycky, etienne.bergeron, etienneb. Pajesz marked an inline comment as done. Pajesz added a comment. Hello! Gonna fix the tests ASAP! Any other suggestions, fixes, improvements considering the checker? https://reviews.llvm.org/D19586 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D19586: Misleading Indentation check
Pajesz updated this revision to Diff 63925. Pajesz added a comment. Minor changes in tests and doc and diff should be full now. http://reviews.llvm.org/D19586 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/MisleadingIndentationCheck.cpp clang-tidy/readability/MisleadingIndentationCheck.h clang-tidy/readability/ReadabilityTidyModule.cpp docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/readability-misleading-indentation.rst test/clang-tidy/readability-misleading-indentation.cpp Index: test/clang-tidy/readability-misleading-indentation.cpp === --- /dev/null +++ test/clang-tidy/readability-misleading-indentation.cpp @@ -0,0 +1,130 @@ +// RUN: %check_clang_tidy %s readability-misleading-indentation %t + +void foo1() {} +void foo2() {} + +int main() { + bool cond1 = true; + bool cond2 = true; + + if (cond1) +if (cond2) + foo1(); +else + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: wrong indentation, 'else' belongs to 'if(cond2)' statement + // CHECK-FIXES: {{^}} // comment1 + + if (cond1) { +if (cond2) + foo1(); + } else +foo2(); // ok, 'else' belongs to 'if(cond1)' statement + + if (cond1) +if (cond2) + foo1(); +else + foo2(); // ok, indentation matches to syntactical structure + + if (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of 'if(cond1)' + // CHECK-FIXES: {{^}} // comment2 + + if (cond2) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of 'if(cond2)' + // CHECK-FIXES: {{^}} // comment3 + + if (cond1) { +foo1(); + } + foo2(); // ok + + if (cond1) +foo1(); + foo2(); // ok + + if (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of 'if(cond1)' + // CHECK-FIXES: {{^}} // comment4 + foo2(); // no need for redundant warning + + if (cond1) { // ok +foo1(); + } else { +foo2(); + } + + if (cond1) +foo1(); + else +foo2(); // ok + + if (cond1) { // ok +foo1(); + } else { +foo2(); + } + + if (cond1) // ok + { +foo1(); + } else { +foo2(); + } + + if (cond1) // ok + { +foo1(); + } else { +foo2(); + } + + while (cond1) { +foo1(); + } + foo2(); // ok + + while (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of 'while(cond1)' + // CHECK-FIXES: {{^}} // comment5 + + while (cond1) { +foo1(); + } + foo2(); // ok + + while (cond1) +foo1(); + foo2(); // ok + + for (;;) { +foo1(); + } + foo2(); // ok + + for (;;) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of 'for(;;)' + // CHECK-FIXES: {{^}} // comment6 + + for (;;) { +foo1(); + } + foo2(); // ok + + for (;;) +foo1(); + foo2(); // ok + + return 0; +} Index: docs/clang-tidy/checks/readability-misleading-indentation.rst === --- /dev/null +++ docs/clang-tidy/checks/readability-misleading-indentation.rst @@ -0,0 +1,36 @@ +.. title:: clang-tidy - readability-misleading-indentation + +readability-misleading-indentation +== + +Description + +Correct indentation helps to understand code. Mismatch of the syntactical +structure and the indentation of the code may reveal serious problems. +Missing braces can also cause some problems in analyzing the code, +therefore it is important to use braces. + +The way to avoid dangling else is to always check that an ``else`` belongs +to the ``if`` that begins in the same column. + +You can omit braces when your inner part of e.g. an ``if`` statement has only +one statement in it. Although in that case you should begin the next statement +in the same column with the ``if``. + +Examples: + +.. code-block:: c++ + + // Dangling else: + + if (cond1) +if (cond2) + foo1(); + else +foo2(); // wrong indentation: else belongs to if(cond2) statement + + // Missing braces + + if (cond1) +foo1(); +foo2(); // not part of if(cond1) Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ readability-identifier-naming readability-implicit-bool-cast readability-inconsistent-declaration-parameter-name + readability-misleading-indentation readability-named-parameter readability-redundant-control-flow readability-redundant-smartptr-get Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++
Re: [PATCH] D19586: Misleading Indentation check
Pajesz updated this revision to Diff 61651. Pajesz added a comment. Checker now works with for and while statements as well, new tests were added, other syntactical and logical updates have been made. http://reviews.llvm.org/D19586 Files: clang-tidy/readability/MisleadingIndentationCheck.h clang-tidy/readability/ReadabilityTidyModule.cpp docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/readability-misleading-indentation.rst test/clang-tidy/readability-misleading-indentation.cpp Index: test/clang-tidy/readability-misleading-indentation.cpp === --- /dev/null +++ test/clang-tidy/readability-misleading-indentation.cpp @@ -0,0 +1,131 @@ +// RUN: %check_clang_tidy %s readability-misleading-indentation %t + +void foo1() {} +void foo2() {} + +int main() { + bool cond1 = true; + bool cond2 = true; + + if (cond1) +if (cond2) + foo1(); +else + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: wrong indentation, else belongs to + // if(cond2) statement + // CHECK-FIXES: {{^}} // comment + + if (cond1) { +if (cond2) + foo1(); + } else +foo2(); // ok, else belongs to if(cond1) statement + + if (cond1) +if (cond2) + foo1(); +else + foo2(); // ok, indentation matches to syntactical structure + + if (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) + // CHECK-FIXES: {{^}} // comment + + if (cond2) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond2) + // CHECK-FIXES: {{^}} // comment + + if (cond1) { +foo1(); + } + foo2(); // ok + + if (cond1) +foo1(); + foo2(); // ok + + if (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) + // CHECK-FIXES: {{^}} // comment + foo2(); // no need for redundant warning + + if (cond1) { // ok +foo1(); + } else { +foo2(); + } + + if (cond1) +foo1(); + else +foo2(); // ok + + if (cond1) { // ok +foo1(); + } else { +foo2(); + } + + if (cond1) // ok + { +foo1(); + } else { +foo2(); + } + + if (cond1) // ok + { +foo1(); + } else { +foo2(); + } + + while (cond1) { +foo1(); + } + foo2(); // ok + + while (cond1) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of while(cond1) + // CHECK-FIXES: {{^}} // comment + + while (cond1) { +foo1(); + } + foo2(); // ok + + while (cond1) +foo1(); + foo2(); // ok + + for (;;) { +foo1(); + } + foo2(); // ok + + for (;;) +foo1(); + foo2(); // comment + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of for(;;) + // CHECK-FIXES: {{^}} // comment + + for (;;) { +foo1(); + } + foo2(); // ok + + for (;;) +foo1(); + foo2(); // ok + + return 0; +} Index: docs/clang-tidy/checks/readability-misleading-indentation.rst === --- /dev/null +++ docs/clang-tidy/checks/readability-misleading-indentation.rst @@ -0,0 +1,36 @@ +.. title:: clang-tidy - readability-misleading-indentation + +readability-misleading-indentation +== + +Description + +Correct indentation helps to understand code. Mismatch of the syntactical +structure and the indentation of the code may reveal serious problems. +Missing braces can also cause some problems in analyzing the code, +therefore it is important to use braces. + +The way to avoid dangling else is to always check that an "else" belongs +to the "if" that begins in the same column. + +You can omit braces when your inner part of e.g. an "if" statement has only +one statement in it. Although in that case you should begin the next statement +in the same column with the "if". + +Examples: + +.. code-block:: c++ + + // Dangling else: + + if (cond1) +if (cond2) + foo1(); + else +foo2(); // wrong indentation: else belongs to if(cond2) statement + + // Missing braces + + if (cond1) +foo1(); +foo2(); // not part of if(cond1) Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ readability-identifier-naming readability-implicit-bool-cast readability-inconsistent-declaration-parameter-name + readability-misleading-indentation readability-named-parameter readability-redundant-control-flow readability-redundant-smartptr-get Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -17,6 +17,7 @@ #include
Re: [PATCH] D19586: Misleading Indentation check
Pajesz updated this revision to Diff 55389. Pajesz marked 5 inline comments as done. Pajesz added a comment. Both dangling else and the other check now ignore one-line if-else statements. Corrected other reviews as well. http://reviews.llvm.org/D19586 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/MisleadingIndentationCheck.cpp clang-tidy/readability/MisleadingIndentationCheck.h clang-tidy/readability/ReadabilityTidyModule.cpp docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/readability-misleading-indentation.rst test/clang-tidy/readability-misleading-indentation.cpp Index: test/clang-tidy/readability-misleading-indentation.cpp === --- /dev/null +++ test/clang-tidy/readability-misleading-indentation.cpp @@ -0,0 +1,62 @@ +// RUN: %check_clang_tidy %s readability-misleading-indentation %t + +void foo1() {} +void foo2() {} + +int main() +{ + bool cond1 = true; + bool cond2 = true; + + if (cond1) + if (cond2) + foo1(); + else + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: wrong indentation, else belongs to if(cond2) statement +// CHECK-FIXES: {{^}} // comment + + if (cond1) { + if (cond2) + foo1(); + } + else + foo2(); // ok, else belongs to if(cond1) statement + + if (cond1) + if (cond2) +foo1(); + else +foo2(); // ok, indentation matches to syntactical structure + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + + if (cond2) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond2) +// CHECK-FIXES: {{^}} // comment + + if (cond1) + { + foo1(); + } + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + foo2(); // no need for redundant warning + + return 0; +} Index: docs/clang-tidy/checks/readability-misleading-indentation.rst === --- /dev/null +++ docs/clang-tidy/checks/readability-misleading-indentation.rst @@ -0,0 +1,30 @@ +.. title:: clang-tidy - readability-misleading-indentation + +readability-misleading-indentation +== + +Description + +Correct indentation helps to understand code. Mismatch of the syntactical structure and the indentation of the code may reveal serious pr + +The way to avoid dangling else is to always check that an "else" belongs to the "if" that begins in the same column. + +You can omit braces when your inner part of e.g. an "if" statement has only one statement in it. Although in that case you should begin t + +Examples: + +.. code-block:: c++ + + // Dangling else: + + if (cond1) +if (cond2) + foo1(); + else +foo2(); // wrong indentation: else belongs to if(cond2) statement + + // Missing braces + + if (cond1) +foo1(); +foo2(); // not part of if(cond1) Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ readability-identifier-naming readability-implicit-bool-cast readability-inconsistent-declaration-parameter-name + readability-misleading-indentation readability-named-parameter readability-redundant-control-flow readability-redundant-smartptr-get Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -17,6 +17,7 @@ #include "IdentifierNamingCheck.h" #include "ImplicitBoolCastCheck.h" #include "InconsistentDeclarationParameterNameCheck.h" +#include "MisleadingIndentationCheck.h" #include "NamedParameterCheck.h" #include "RedundantControlFlowCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -45,6 +46,8 @@ "readability-implicit-bool-cast"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); +CheckFactories.registerCheck( +"readability-misleading-indentation"); CheckFactories.registerCheck( "readability-named-parameter"); CheckFactories.registerCheck( Index: clang-tidy/readability/MisleadingIndentationCheck.h === --- /dev/null +++ clang-tidy/readability/MisleadingIndentationCheck.h @@ -0,0 +1,38 @@ +//===--- MisleadingIndentationCheck.h - clang-tidy---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois
Re: [PATCH] D19586: Misleading Indentation check
Pajesz updated this revision to Diff 55247. Pajesz added a comment. Probably fixed the broken patch. http://reviews.llvm.org/D19586 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/MisleadingIndentationCheck.cpp clang-tidy/readability/MisleadingIndentationCheck.h clang-tidy/readability/ReadabilityTidyModule.cpp docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/readability-misleading-indentation.r test/clang-tidy/readability-misleading-indentation.cpp Index: test/clang-tidy/readability-misleading-indentation.cpp === --- /dev/null +++ test/clang-tidy/readability-misleading-indentation.cpp @@ -0,0 +1,62 @@ +// RUN: %check_clang_tidy %s readability-misleading-indentation %t + +void foo1() {} +void foo2() {} + +int main() +{ + bool cond1 = true; + bool cond2 = true; + + if (cond1) + if (cond2) + foo1(); + else + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: wrong indentation, else belongs to if(cond2) statement +// CHECK-FIXES: {{^}} // comment + + if (cond1) { + if (cond2) + foo1(); + } + else + foo2(); // ok, else belongs to if(cond1) statement + + if (cond1) + if (cond2) +foo1(); + else +foo2(); // ok, indentation matches to syntactical structure + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + + if (cond2) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond2) +// CHECK-FIXES: {{^}} // comment + + if (cond1) + { + foo1(); + } + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + foo2(); // no need for redundant warning + + return 0; +} Index: docs/clang-tidy/checks/readability-misleading-indentation.r === --- /dev/null +++ docs/clang-tidy/checks/readability-misleading-indentation.r @@ -0,0 +1,30 @@ +.. title:: clang-tidy - readability-misleading-indentation + +readability-misleading-indentation +== + +Description + +Correct indentation helps to understand code. Mismatch of the syntactical structure and the indentation of the code may reveal serious + +The way to avoid dangling else is to always check that an else belongs to the if that begins in the same column. + +You can omit braces when your inner part of e.g. an IF statement has only one statement in it. Although in that case you should begin t + +Examples: + +.. code-block:: c++ + + // Dangling else: + + if (cond1) +if (cond2) + foo1(); + else +foo2(); // wrong indentation: else belongs to if(cond2) statement + + // Missing braces + + if (cond1) +foo1(); +foo2(); // not part of if(cond1) Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ readability-identifier-naming readability-implicit-bool-cast readability-inconsistent-declaration-parameter-name + readability-misleading-indentation readability-named-parameter readability-redundant-control-flow readability-redundant-smartptr-get Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -17,6 +17,7 @@ #include "IdentifierNamingCheck.h" #include "ImplicitBoolCastCheck.h" #include "InconsistentDeclarationParameterNameCheck.h" +#include "MisleadingIndentationCheck.h" #include "NamedParameterCheck.h" #include "RedundantControlFlowCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -45,6 +46,8 @@ "readability-implicit-bool-cast"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); +CheckFactories.registerCheck( +"readability-misleading-indentation"); CheckFactories.registerCheck( "readability-named-parameter"); CheckFactories.registerCheck( Index: clang-tidy/readability/MisleadingIndentationCheck.h === --- /dev/null +++ clang-tidy/readability/MisleadingIndentationCheck.h @@ -0,0 +1,38 @@ +//===--- MisleadingIndentationCheck.h - clang-tidy---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +//
[PATCH] D19586: Misleading Indentation check
Pajesz created this revision. Pajesz added a reviewer: alexfh. Pajesz added subscribers: cfe-commits, xazax.hun. This is a clang-tidy check for llvm. It checks for dangling else and for possible misleading indentation due to omitted braces. http://reviews.llvm.org/D19586 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/Mislead clang-tidy/readability/Misleadin clang-tidy/readability/ReadabilityT docs/clang-tidy/checks docs/clang-tidy/checks/list.rst test/clang-tidy/readability-m Index: test/clang-tidy/readability-m === --- /dev/null +++ test/clang-tidy/readability-m @@ -0,0 +1,62 @@ +// RUN: %check_clang_tidy %s readability-misleading-indentation %t + +void foo1() {} +void foo2() {} + +int main() +{ + bool cond1 = true; + bool cond2 = true; + + if (cond1) + if (cond2) + foo1(); + else + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: wrong indentation, else belongs to if(cond2) statement +// CHECK-FIXES: {{^}} // comment + + if (cond1) { + if (cond2) + foo1(); + } + else + foo2(); // ok, else belongs to if(cond1) statement + + if (cond1) + if (cond2) +foo1(); + else +foo2(); // ok, indentation matches to syntactical structure + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + + if (cond2) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond2) +// CHECK-FIXES: {{^}} // comment + + if (cond1) + { + foo1(); + } + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // ok + + if (cond1) + foo1(); + foo2(); // comment +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: not part of if(cond1) +// CHECK-FIXES: {{^}} // comment + foo2(); // no need for redundant warning + + return 0; +} Index: docs/clang-tidy/checks === --- /dev/null +++ docs/clang-tidy/checks @@ -0,0 +1,30 @@ +.. title:: clang-tidy - readability-misleading-indentation + +readability-misleading-indentation +== + +Description + +Correct indentation helps to understand code. Mismatch of the syntactical structure and the indent + +The way to avoid dangling else is to always check that an else belongs to the if that begins in th + +You can omit braces when your inner part of e.g. an IF statement has only one statement in it. Alt + +Examples: + +.. code-block:: c++ + + // Dangling else: + + if (cond1) +if (cond2) + foo1(); + else +foo2(); // wrong indentation: else belongs to if(cond2) statement + + // Missing braces + + if (cond1) +foo1(); +foo2(); // not part of if(cond1) Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ readability-identifier-naming readability-implicit-bool-cast readability-inconsistent-declaration-parameter-name + readability-misleading-indentation readability-named-parameter readability-redundant-control-flow readability-redundant-smartptr-get Index: clang-tidy/readability/ReadabilityT === --- clang-tidy/readability/ReadabilityT +++ clang-tidy/readability/ReadabilityT @@ -17,6 +17,7 @@ #include "IdentifierNamingCheck.h" #include "ImplicitBoolCastCheck.h" #include "InconsistentDeclarationParameterNameCheck.h" +#include "MisleadingIndentationCheck.h" #include "NamedParameterCheck.h" #include "RedundantControlFlowCheck.h" #include "RedundantSmartptrGetCheck.h" @@ -45,6 +46,8 @@ "readability-implicit-bool-cast"); CheckFactories.registerCheck( "readability-inconsistent-declaration-parameter-name"); +CheckFactories.registerCheck( +"readability-misleading-indentation"); CheckFactories.registerCheck( "readability-named-parameter"); CheckFactories.registerCheck( Index: clang-tidy/readability/Misleadin === --- /dev/null +++ clang-tidy/readability/Misleadin @@ -0,0 +1,38 @@ +//===--- MisleadingIndentationCheck.h - clang-tidy---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MISLEADING_INDENTATION_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MISLEADING_INDENTATION_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +///