Skip to content

Commit 61e9efe

Browse files
added more basic syntax errors in c++ (imDarshanGK#86)
* feat: add comprehensive C++ bug patterns (arrays, vectors, pointers, loops) * feat: enhance C++ syntax and logic bug detection * test: add tests for C++ syntax error detection --------- Co-authored-by: Darshan G K <122042809+imDarshanGK@users.noreply.github.com>
1 parent 2d6e762 commit 61e9efe

3 files changed

Lines changed: 119 additions & 13 deletions

File tree

.github/pull_request_template.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
## Summary
1+
## Description
2+
<!-- What does this PR do? Be specific. -->
23

3-
Describe what this PR changes.
4-
5-
## Type of Change
4+
## Related Issue
5+
<!-- Required — link the issue this PR fixes -->
6+
Fixes #
67

8+
## Type of change
79
- [ ] Bug fix
8-
- [ ] New feature
10+
- [ ] New feature / enhancement
911
- [ ] Documentation update
12+
- [ ] Test addition
1013
- [ ] Refactor
1114

12-
## Testing
13-
14-
- [ ] I ran backend tests (`pytest -q`)
15-
- [ ] I tested frontend behavior manually
16-
1715
## Checklist
16+
<!-- All boxes must be checked before requesting review -->
17+
- [ ] I have read [CONTRIBUTING.md](../CONTRIBUTING.md)
18+
- [ ] My branch is up to date with `main`
19+
- [ ] I have run `pytest -v` and all tests pass
20+
- [ ] I have not introduced duplicate issues or features
21+
- [ ] My PR title follows the format: `feat/fix/docs/test: short description`
22+
- [ ] I have added tests for new features (Level 2 and 3 issues)
23+
- [ ] No hardcoded secrets or API keys in my code
24+
- [ ] This PR is linked to a GSSoC 2026 issue
25+
26+
## Screenshots (if frontend change)
27+
<!-- Add before/after screenshots -->
1828

19-
- [ ] Code is readable and beginner-friendly
20-
- [ ] No fake or placeholder behavior introduced
21-
- [ ] Documentation updated if needed
29+
## Test evidence
30+
<!-- Paste pytest output here -->
31+
```bash
32+
pytest -v
33+
# paste output
34+
```

backend/app/services/code_assistant.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,79 @@ class BugPattern:
325325
"Comparing signed `int` with unsigned `.size()` — undefined behavior on overflow.",
326326
"Cast to `(int)` or use `std::ssize()` (C++20).",
327327
"warning", ["C++"]),
328+
BugPattern("Void Main", r"\bvoid\s+main\s*\(",
329+
"`void main()` is non-standard C++ and results in a compilation error.",
330+
"Use `int main()` and return 0 at the end.",
331+
"error", ["C++"]),
332+
BugPattern("Single Quotes for String", r"'[^'\\]{2,}'",
333+
"Single quotes are used for strings. In C++, single quotes are strictly for single characters.",
334+
"Use double quotes `\"...\"` for string literals.",
335+
"error", ["C++"]),
336+
BugPattern("Missing Semicolon", r"^(?!.*\b(if|for|while|switch|catch)\b)(?!.*[{}#])(?!^\s*(int|float|double|char|long|short|bool|string|void)\s+\w+\s*\([^)]*\)\s*$).*\b(cout|cin|return|int|float|double|char|long|short|bool|string)\b[^;]*[^\s;]\s*$",
337+
"Missing semicolon at the end of the statement.",
338+
"Add a semicolon `;` at the end of the line.",
339+
"error", ["C++"]),
340+
341+
BugPattern("Incomplete Assignment", r"=\s*$",
342+
"Statement ends abruptly with an assignment operator.",
343+
"Provide a value for the assignment.",
344+
"error", ["C++", "Java", "Python", "JavaScript", "TypeScript"]),
345+
BugPattern("Semicolon After Loop", r"\b(for|while)\s*\([^)]*\)\s*;",
346+
"Semicolon immediately after loop condition creates an empty loop body.",
347+
"Remove the semicolon so the loop executes the intended block.",
348+
"error", ["C++", "Java", "JavaScript", "TypeScript"]),
349+
BugPattern("Type Mismatch: String to Int", r"\b(int|long|short)\s+[a-zA-Z_]\w*\s*=\s*\"[^\"]*\"",
350+
"Attempting to assign a string literal to an integer variable.",
351+
"Use `std::string` for strings, or parse the string using `std::stoi`.",
352+
"error", ["C++", "Java"]),
353+
BugPattern("Uninitialized Variable Risk", r"^\s*(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*;\s*$",
354+
"Variable is declared without an initial value. Using it before assignment causes undefined behavior.",
355+
"Initialize the variable upon declaration (e.g., `= 0;`).",
356+
"warning", ["C++"]),
357+
BugPattern("Float Equality", r"==\s*\d+\.\d+",
358+
"Directly comparing floating point numbers with `==` is unsafe due to precision issues.",
359+
"Compare the absolute difference with an epsilon value (e.g., `abs(a - b) < 1e-9`).",
360+
"warning", ["C++", "Java", "Python", "JavaScript"]),
361+
BugPattern("Variable Length Array", r"\b(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*\[\s*[a-zA-Z_]\w*\s*\]\s*;",
362+
"Using a variable to define an array size (VLA) is not standard C++ and fails on some compilers.",
363+
"Use `std::vector` for dynamically sized arrays.",
364+
"error", ["C++"]),
365+
BugPattern("Negative Array Index", r"\[\s*-\s*\d+\s*\]",
366+
"Hardcoded negative index detected. In C++ this accesses memory out of bounds.",
367+
"Ensure array indices are 0 or greater.",
368+
"error", ["C++", "Java", "JavaScript", "TypeScript"]),
369+
BugPattern("C-Style Array", r"\b(int|float|double|char|long|short)\s+[a-zA-Z_]\w*\s*\[\s*\d+\s*\]\s*;",
370+
"Raw C-style arrays do not carry their size and unsafely decay to pointers.",
371+
"Use `std::array<T, N>` for fixed-size arrays.",
372+
"info", ["C++"]),
373+
BugPattern("Vector Pass by Value", r"\b\w+\s*\(\s*std::vector\s*<\s*[\w:]+\s*>\s+\w+\s*[,)]",
374+
"Passing a `std::vector` by value creates a full, expensive copy.",
375+
"Pass by const reference (e.g., `const std::vector<T>&`) unless you need to mutate a copy.",
376+
"warning", ["C++"]),
377+
BugPattern("Vector Unsigned Underflow", r"\.size\(\)\s*-\s*1",
378+
"Vector `.size()` is unsigned. If empty, subtracting 1 causes an underflow to a huge number.",
379+
"Always check `.empty()` first, or cast size to a signed integer.",
380+
"error", ["C++"]),
381+
BugPattern("malloc in C++", r"\bmalloc\s*\(",
382+
"C-style `malloc` allocates memory but does not call C++ constructors.",
383+
"Use `new` or `std::make_unique` instead.",
384+
"error", ["C++"]),
385+
BugPattern("Dangling Pointer Return", r"return\s+&\s*\w+\s*;",
386+
"Returning the address of a local variable creates a dangling pointer.",
387+
"Return by value, or allocate on the heap and return a smart pointer.",
388+
"error", ["C++"]),
389+
BugPattern("Missing Hash in Include", r"^\s*include\s*[<\"]",
390+
"Preprocessor directives must start with a `#`.",
391+
"Add a `#` at the beginning of the line (e.g., `#include`).",
392+
"error", ["C++"]),
393+
BugPattern("Semicolon in Condition", r"\b(if|while|switch)\s*\([^)]*;\s*\)",
394+
"Condition blocks (if, while, switch) should not contain semicolons.",
395+
"Remove the semicolon from inside the parentheses.",
396+
"error", ["C++", "Java", "JavaScript", "TypeScript"]),
397+
BugPattern("Malformed For-Loop", r"\bfor\s*\([^;:]*(?:;[^;:]*)?\)",
398+
"A traditional for-loop must contain exactly two semicolons.",
399+
"Ensure you have two semicolons separating the initialization, condition, and increment statements.",
400+
"error", ["C++", "Java", "JavaScript", "TypeScript"]),
328401

329402
# ── PHP ──
330403
BugPattern("PHP MySQL Deprecated", r"\bmysql_\w+\s*\(",
@@ -601,6 +674,17 @@ def run_suggestions(code: str, language: str) -> dict:
601674
"priority": "medium",
602675
})
603676

677+
# std::endl Performance (only if in a file with loops)
678+
if language == "C++":
679+
if re.search(r"<<\s*(std::)?endl\b", code) and re.search(r"\b(for|while)\b", code):
680+
suggestions.append({
681+
"category": "Performance",
682+
"description": "Code contains both a loop and `std::endl`. If `std::endl` is used inside the loop, it flushes the buffer on every iteration, severely degrading performance.",
683+
"example": "std::cout << value << '\\n';",
684+
"priority": "medium",
685+
})
686+
687+
# Score
604688
# Score calculation
605689
deductions = sum({"high": 15, "medium": 7, "low": 3}.get(s["priority"], 5) for s in suggestions)
606690
score = max(0, min(100, 100 - deductions))

backend/tests/test_endpoints.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ def test_debug_kotlin():
460460
d = r.json()
461461
assert d is not None
462462

463+
def test_debug_cpp_syntax_errors():
464+
code = "void main() {\n cout << 'Hello World'\n}"
465+
r = client.post("/debugging/", json={"code": code, "language": "cpp"})
466+
assert r.status_code == 200
467+
types = [i["type"] for i in r.json()["issues"]]
468+
assert "Void Main" in types
469+
assert "Single Quotes for String" in types
470+
assert "Missing Semicolon" in types
471+
463472
def test_debug_issue_has_required_fields():
464473
r = client.post("/debugging/", json={"code": PYTHON_BUGGY})
465474
assert r.status_code == 200

0 commit comments

Comments
 (0)