Skip to content

Commit

Permalink
Add example to showcase the use of fullName in a rule
Browse files Browse the repository at this point in the history
  • Loading branch information
kunaltyagi committed Oct 20, 2024
1 parent d199076 commit 40537d2
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 7 deletions.
11 changes: 4 additions & 7 deletions nsiqcppstyle_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,9 +1216,8 @@ def ConstructContextInfo(lexer):
prevName = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess()
if prevName is not None:
if prevName.type == "DOUBLECOLON":
fullName = (
f"{lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value}::{fullName}"
)
value_to_prepend = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value
fullName = f"{value_to_prepend}::{fullName}"
else:
break
else:
Expand All @@ -1230,10 +1229,8 @@ def ConstructContextInfo(lexer):
if prevName.type == "NOT":
fullName = "~" + fullName
elif prevName.type == "DOUBLECOLON":
fullName = "::" + fullName
fullName = (
lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value + fullName
)
value_to_prepend = lexer.GetPrevTokenSkipWhiteSpaceAndCommentAndPreprocess().value
fullName = f"{value_to_prepend}::{fullName}"
else:
break
else:
Expand Down
111 changes: 111 additions & 0 deletions rules/RULE_11_A_add_class_scope_in_fn_declarations_and_definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""
Ensure that the functions declarations/definitions in the class/struct scope are scoped
== Violation ==
class A
{
bool GetSth(); <== Violation. Should be A::GetSth.
};
== Good ==
class A
{
bool ::GetSth(); <== OK.
public:
bool A::GetSthElse(); <== OK.
};
"""

from nsiqcppstyle_reporter import *
from nsiqcppstyle_rulehelper import *
from nsiqcppstyle_rulemanager import *
from nsiqunittest.nsiqcppstyle_unittestbase import *


def RunRule(lexer, fullName, decl, contextStack, context):
t = lexer.GetCurToken()
value = t.value
upperBlock = contextStack.SigPeek()
if upperBlock is None:
return
if upperBlock.type in ["CLASS_BLOCK", "STRUCT_BLOCK"] and value == fullName:
class_name = upperBlock.name.rsplit("::")[-1]
nsiqcppstyle_reporter.Error(t, __name__, f"Function name({fullName}) should include local scope({class_name})")


ruleManager.AddFunctionNameRule(RunRule)

##########################################################################
# Unit Test
##########################################################################


class testRule(nct):
def setUpRule(self):
ruleManager.AddFunctionNameRule(RunRule)
global currentVisibility
currentVisibility = False

def test1(self):
self.Analyze(
"test/thisFile.c",
"""
bool CanHave() {
}""",
)
self.ExpectSuccess(__name__)

def test2(self):
self.Analyze(
"test/thisFile.c",
"""
class TT::K {
bool CanHave() {
}
}""",
)
self.ExpectError(__name__)

def test3(self):
self.Analyze(
"test/thisFile.c",
"""
class TT::K {
bool K::CanHave() {
}
}""",
)
self.ExpectSuccess(__name__)

def test4(self):
self.Analyze(
"test/thisFile.c",
"""
class TT::K {
bool TT::K::CanHave() {
}
}""",
)
self.ExpectSuccess(__name__)

def test5(self):
self.Analyze(
"test/thisFile.c",
"""
class TT::K {
K& operator++();
}""",
)
self.ExpectError(__name__)

def test6(self):
self.Analyze(
"test/thisFile.c",
"""
class TT::K {
K& K::operator++();
}""",
)
self.ExpectSuccess(__name__)

0 comments on commit 40537d2

Please sign in to comment.