Skip to content

Commit

Permalink
ctags: fix improper use of "const" type qualifier
Browse files Browse the repository at this point in the history
The external declaration of "File" in read.h (defined in read.c) was
improperly tagged as "const" for it not to be modifiable outside of
read.c.  Although it is good to protect this global variable against
improper modification, the use of "const" here makes it perfectly valid
for the compiler to assume that the fields in this structure never
changes during runtime, thus allowing it to do optimizations on this
assumption.  However, this assumption is wrong because this structure
actually gets modified by many read.c's functions, and thus possibly
lead to improper and unexpected behavior if the compiler sees a window
for optimizing fields access.

Moreover, protecting "File" as it was with the "const" type qualifier
required a hack to be able to include read.h in read.c since "const"
and non-"const" declarations conflicts.

Actually, at least the JavaScript parser did suffer of the issue,
because it calls getSourceLineNumber() macro (expanding to a direct
"File" member access) several times in one single function, making it
easy for the compilers to cache the value as an optimization.  Both GCC
and CLang showed this behavior with optimization enabled.  As a result,
the line numbers of JavaScript tags were often incorrect.
  • Loading branch information
b4n committed Sep 22, 2012
1 parent 9ef34bb commit 772509e
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions tagmanager/ctags/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
#ifndef _READ_H
#define _READ_H

#if defined(FILE_WRITE) || defined(VAXC)
# define CONST_FILE
#else
# define CONST_FILE const
#endif

/*
* INCLUDE FILES
*/
Expand Down Expand Up @@ -96,7 +90,8 @@ typedef struct sInputFile {
/*
* GLOBAL VARIABLES
*/
extern CONST_FILE inputFile File;
/* should not be modified externally */
extern inputFile File;

/*
* FUNCTION PROTOTYPES
Expand Down

0 comments on commit 772509e

Please sign in to comment.