From fce0c9102c4ced0d127b1cd3ba7dbe8a4d2dff04 Mon Sep 17 00:00:00 2001 From: Ricardo Rodrigues Lucca Date: Thu, 2 Jul 2015 10:46:38 -0300 Subject: [PATCH] Fixing bug with define _ Some very smart people like do a definition of '_' to be an alias to a field in a struct. Fixes #543 --- .../src/main/java/org/sonar/cxx/checks/ReservedNamesCheck.java | 2 +- cxx-checks/src/test/resources/checks/ReservedNamesCheck.cc | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cxx-checks/src/main/java/org/sonar/cxx/checks/ReservedNamesCheck.java b/cxx-checks/src/main/java/org/sonar/cxx/checks/ReservedNamesCheck.java index bc25ade466..2aab37a8b1 100644 --- a/cxx-checks/src/main/java/org/sonar/cxx/checks/ReservedNamesCheck.java +++ b/cxx-checks/src/main/java/org/sonar/cxx/checks/ReservedNamesCheck.java @@ -70,7 +70,7 @@ public void visitFile(AstNode astNode) { String[] sub = line.split("^\\s*#define\\s+", 2); if (sub.length>1) { String name = sub[1].split("[\\s(]",2)[0]; - if (name.startsWith("_") && Character.isUpperCase(name.charAt(1))) { + if (name.startsWith("_") && name.length() > 1 && Character.isUpperCase(name.charAt(1))) { getContext().createLineViolation(this, "Reserved name used for macro (begins with underscore followed by a capital letter)", nr); } else if (name.contains("__")) { diff --git a/cxx-checks/src/test/resources/checks/ReservedNamesCheck.cc b/cxx-checks/src/test/resources/checks/ReservedNamesCheck.cc index eb875cd465..1e426b76fa 100644 --- a/cxx-checks/src/test/resources/checks/ReservedNamesCheck.cc +++ b/cxx-checks/src/test/resources/checks/ReservedNamesCheck.cc @@ -23,7 +23,8 @@ class myData { #define _test #define TRUE_TEST /*#define TRUE this is not an error...*/ +#define _ ks._ int main() { -} \ No newline at end of file +}