Skip to content

Commit

Permalink
two new compiler error explanations added
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-taylor committed Apr 27, 2019
1 parent 49b72b8 commit 9b754d9
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 4 deletions.
44 changes: 42 additions & 2 deletions compiler_explanations.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def get_short_explanation(self, message, colorize_output):
),

Explanation(
label = 'dcc-uninitialized-local-variable',
label = 'uninitialized-local-variable',

regex = r"'(.*)' is used uninitialized in this function",

Expand All @@ -398,7 +398,7 @@ def get_short_explanation(self, message, colorize_output):
),

Explanation(
label = 'dcc-function-variable-clash',
label = 'function-variable-clash',

regex = r"called object type '.*' is not a function or function pointer",

Expand All @@ -418,6 +418,46 @@ def get_short_explanation(self, message, colorize_output):
""",
),


Explanation(
label = 'function-definition-not-allowed-here',

regex = r"function definition is not allowed here",

precondition = lambda message, match: message.line_number and int(message.line_number) > 1,

long_explanation = True,

explanation = """There is likely a closing brace (curly bracket) missing before line {line_number}.
Is a {emphasize('} missing')} in the previous function?""",

no_following_explanations = True,

reproduce = """
int f(int a) {
return a;
int main(void) {
return f(0);
}
""",
),

Explanation(
label = 'indirection-requires-pointer-operand',

regex = r"indirection requires pointer operand \('(.*)' invalid\)",

explanation = """ you are trying to use '{emphasize(underlined_word)}' as a pointer.
You can not do this because '{emphasize(underlined_word)}' is of type {emphasize(match.group(1))}.
""",

reproduce = """
int main(int argc, char *argv[]) {
return *argc;
}
""",
),
]

def extract_system_include_file(string):
Expand Down
33 changes: 33 additions & 0 deletions docs/function-definition-not-allowed-here.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

If you forget a curly bracket, e.g.
```c
int sum(int x, int y) {
if (x > y) {
return x + y;
} else {
return x - y;
// <-- missing closing curly bracket
}

int f(int x) {
return sum(x, x);
}
```
The compiler will give an error when the next function definition starts.
You can fix by adding the missing curly bracket (brace):
```c
int sum(int x, int y) {
if (x > y) {
return x + y;
} else {
return x - y;
}
}
int f(int x) {
return sum(x, x);
}
```
File renamed without changes.
4 changes: 2 additions & 2 deletions drive_gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def drive_gdb():
global debug
debug = int(os.environ.get('DCC_DEBUG', '0'))
output_stream = os.fdopen(3, "w")
output_stream = os.fdopen(3, "w", encoding='utf-8', errors='replace')
colorize_output = output_stream.isatty() or os.environ.get('DCC_COLORIZE_OUTPUT', False)
if colorize_output:
color = colors.color
Expand Down Expand Up @@ -307,7 +307,7 @@ def fileline(filename, line_number, clean=False):
return clean_c_source(source[filename][line_number - 1])
return source[filename][line_number - 1]
try:
with open(filename) as f:
with open(filename, encoding='utf-8', errors='replace') as f:
source[filename] = f.readlines()
for line in source[filename]:
m = re.match(r'^\s*#\s*define\s*(\w+)\s*(.*\S)', line)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/function-definition-not-allowed-here.c:5:16: error: function definition is not allowed here
int main(void) {
^
dcc explanation: There is likely a closing brace (curly bracket) missing before line 5.
Is a } missing in the previous function?
See more information here: https://comp1511unsw.github.io/dcc/function-definition-not-allowed-here.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/extracted_compile_time_tests/function-variable-clash.c:4:13: error: called object type 'int' is not a function or function pointer
return main();
~~~~^
dcc explanation: 'main' is the name of a variable but you are trying to call it as a function.
If 'main' is also the name of a function, you can avoid the clash,
by changing the name of the variable 'main' to something else.
See more information here: https://comp1511unsw.github.io/dcc/function-variable-clash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/indirection-requires-pointer-operand.c:3:9: error: indirection requires pointer operand ('int' invalid)
return *argc;
^~~~~
dcc explanation: you are trying to use 'argc' as a pointer.
You can not do this because 'argc' is of type int.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/uninitialized-local-variable.c: In function ‘main’:
tests/extracted_compile_time_tests/uninitialized-local-variable.c:4:10: warning: ‘a[0]’ is used uninitialized in this function [-Wuninitialized]
return a[0];
~^~~
dcc explanation: You are using the value of the variable a[0] before assigning a value to a[0].

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/function-definition-not-allowed-here.c:5:16: error: function definition is not allowed here
int main(void) {
^
dcc explanation: There is likely a closing brace (curly bracket) missing before line 5.
Is a } missing in the previous function?
See more information here: https://comp1511unsw.github.io/dcc/function-definition-not-allowed-here.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/extracted_compile_time_tests/function-variable-clash.c:4:13: error: called object type 'int' is not a function or function pointer
return main();
~~~~^
dcc explanation: 'main' is the name of a variable but you are trying to call it as a function.
If 'main' is also the name of a function, you can avoid the clash,
by changing the name of the variable 'main' to something else.
See more information here: https://comp1511unsw.github.io/dcc/function-variable-clash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/indirection-requires-pointer-operand.c:3:9: error: indirection requires pointer operand ('int' invalid)
return *argc;
^~~~~
dcc explanation: you are trying to use 'argc' as a pointer.
You can not do this because 'argc' is of type int.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/uninitialized-local-variable.c: In function ‘main’:
tests/extracted_compile_time_tests/uninitialized-local-variable.c:4:10: warning: ‘a[0]’ is used uninitialized in this function [-Wuninitialized]
return a[0];
~^~~
dcc explanation: You are using the value of the variable a[0] before assigning a value to a[0].

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/function-definition-not-allowed-here.c:5:16: error: function definition is not allowed here
int main(void) {
^
dcc explanation: There is likely a closing brace (curly bracket) missing before line 5.
Is a } missing in the previous function?
See more information here: https://comp1511unsw.github.io/dcc/function-definition-not-allowed-here.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/extracted_compile_time_tests/function-variable-clash.c:4:13: error: called object type 'int' is not a function or function pointer
return main();
~~~~^
dcc explanation: 'main' is the name of a variable but you are trying to call it as a function.
If 'main' is also the name of a function, you can avoid the clash,
by changing the name of the variable 'main' to something else.
See more information here: https://comp1511unsw.github.io/dcc/function-variable-clash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/indirection-requires-pointer-operand.c:3:9: error: indirection requires pointer operand ('int' invalid)
return *argc;
^~~~~
dcc explanation: you are trying to use 'argc' as a pointer.
You can not do this because 'argc' is of type int.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests/extracted_compile_time_tests/uninitialized-local-variable.c: In function ‘main’:
tests/extracted_compile_time_tests/uninitialized-local-variable.c:4:10: warning: ‘a[0]’ is used uninitialized in this function [-Wuninitialized]
return a[0];
~^~~
dcc explanation: You are using the value of the variable a[0] before assigning a value to a[0].

0 comments on commit 9b754d9

Please sign in to comment.