diff options
| author | Richard M. Stallman | 2007-11-24 16:00:57 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2007-11-24 16:00:57 +0000 |
| commit | a0925923a22ca3fa3055e27396785262039ab247 (patch) | |
| tree | dccd131027752885b09effe3731c4cfc5257bd76 | |
| parent | fc37ae728d88a115c8e1b5c2d9d7705284e5f40e (diff) | |
| download | emacs-a0925923a22ca3fa3055e27396785262039ab247.tar.gz emacs-a0925923a22ca3fa3055e27396785262039ab247.zip | |
(Declaring Functions): Clarify previous change.
| -rw-r--r-- | doc/lispref/ChangeLog | 6 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 84 |
2 files changed, 56 insertions, 34 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 16c78f429ea..05245e8126a 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2007-11-24 Richard Stallman <rms@gnu.org> | ||
| 2 | |||
| 3 | * functions.texi (Declaring Functions): Clarify previous change. | ||
| 4 | |||
| 5 | * compile.texi (Compiler Errors): Clarify previous change. | ||
| 6 | |||
| 1 | 2007-11-24 Glenn Morris <rgm@gnu.org> | 7 | 2007-11-24 Glenn Morris <rgm@gnu.org> |
| 2 | 8 | ||
| 3 | * functions.texi (Declaring Functions): New section. | 9 | * functions.texi (Declaring Functions): New section. |
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 1ea2defb059..b8fc9a41954 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi | |||
| @@ -1228,50 +1228,66 @@ following the definition, just like macros. | |||
| 1228 | @cindex function declaration | 1228 | @cindex function declaration |
| 1229 | @cindex declaring functions | 1229 | @cindex declaring functions |
| 1230 | 1230 | ||
| 1231 | Byte-compiling a file often produces warnings about functions that are | 1231 | Byte-compiling a file often produces warnings about functions that the |
| 1232 | @samp{not known to be defined} (@pxref{Compiler Errors}). The compiler | 1232 | compiler doesn't know about (@pxref{Compiler Errors}). Sometimes this |
| 1233 | is technically correct, but the code is usually such that when it | 1233 | indicates a real problem, but usually the functions in question are |
| 1234 | actually runs, the function @emph{will} be defined. For example, | 1234 | defined in other files which would be loaded if that code is run. For |
| 1235 | byte-compiling @file{fortran.el} used to warn: | 1235 | example, byte-compiling @file{fortran.el} used to warn: |
| 1236 | 1236 | ||
| 1237 | @example | 1237 | @smallexample |
| 1238 | In end of data: | 1238 | In end of data: |
| 1239 | fortran.el:2152:1:Warning: the function `gud-find-c-expr' is not known | 1239 | fortran.el:2152:1:Warning: the function `gud-find-c-expr' is not known to be defined. |
| 1240 | to be defined. | 1240 | @end smallexample |
| 1241 | @end example | ||
| 1242 | 1241 | ||
| 1243 | But @code{gud-find-c-expr} is only used in the function that Fortran | 1242 | In fact, @code{gud-find-c-expr} is only used in the function that |
| 1244 | mode uses for the local value of @code{gud-find-expr-function}. This | 1243 | Fortran mode uses for the local value of |
| 1245 | would only ever be called from gud, so the warning can safely be | 1244 | @code{gud-find-expr-function}, which is a callback from GUD; if it is |
| 1246 | suppressed. It's nice to do this, so that real warnings are more | 1245 | called, the GUD functions will be loaded. When you know that such a |
| 1247 | visible. | 1246 | warning does not indicate a real problem, it is good to suppress the |
| 1247 | warning. That makes new warnings which might mean real problems more | ||
| 1248 | visible. You do that with @code{declare-function}. | ||
| 1248 | 1249 | ||
| 1249 | All you need to do is add a @code{declare-function} statement before the | 1250 | All you need to do is add a @code{declare-function} statement before the |
| 1250 | first use of the function in question: | 1251 | first use of the function in question: |
| 1251 | 1252 | ||
| 1252 | @example | 1253 | @smallexample |
| 1253 | (declare-function gud-find-c-expr "gud.el" nil) | 1254 | (declare-function gud-find-c-expr "gud.el" nil) |
| 1254 | @end example | 1255 | @end smalllexample |
| 1255 | 1256 | ||
| 1256 | This says that @code{gud-find-c-expr} is defined in @file{gud.el} (the | 1257 | This says that @code{gud-find-c-expr} is defined in @file{gud.el} (the |
| 1257 | `.el' can be omitted). The file is searched for using | 1258 | @samp{.el} can be omitted). The compiler takes for granted that that file |
| 1258 | @code{locate-library}, and failing that it is expanded relative to the | 1259 | really defines the function, and does not check. |
| 1259 | file containing the @code{declare-function} statement. Functions | 1260 | |
| 1260 | defined in C can also be declared - @file{.c} files are expanded | 1261 | The optional third argument specifies the argument list of |
| 1261 | relative to the Emacs @file{src/} directory. | 1262 | @code{gud-find-c-expr}. In this case, it takes no arguments |
| 1262 | 1263 | (@code{nil} is different from not specifying a value). In other | |
| 1263 | The optional third argument specifies the argument list of | 1264 | cases, this might be something like @code{(file &optional overwrite)}. |
| 1264 | @code{gud-find-c-expr}. In this case, it takes no arguments (@code{nil} | 1265 | You don't have to specify the argument list, but if you do the |
| 1265 | is different from not specifying a value). In other cases, this might | 1266 | byte compiler can check that the calls match the declaration. |
| 1266 | be something like @code{(file &optional overwrite)}. You don't have to | 1267 | |
| 1267 | specify the argument list, but if you do the byte-compiler will check | 1268 | @defmac declare-function function file arglist |
| 1268 | that the calls match the declaration. | 1269 | Tell the byte compiler to assume that @var{function} is defined, with |
| 1269 | 1270 | arguments @var{arglist}, and that the definition should come from | |
| 1270 | The functions @code{check-declare-file} and | 1271 | the file @var{file}. |
| 1271 | @code{check-declare-directory} check that all the | 1272 | @end defmac |
| 1272 | @code{declare-function} statements in a file or directory are true | 1273 | |
| 1273 | (i.e. that the functions @emph{are} defined in the specified files, and | 1274 | To verify that these functions really are declared where |
| 1274 | have matching argument lists, if these were specified). | 1275 | @code{declare-function} says they are, use @code{check-declare-file} |
| 1276 | to check all @code{declare-function} calls in one source file, or use | ||
| 1277 | @code{check-declare-directory} check all the files in and under a | ||
| 1278 | certain directory. | ||
| 1279 | |||
| 1280 | These commands find the file that ought to contain a function's | ||
| 1281 | definition using @code{locate-library}; if that finds no file, they | ||
| 1282 | expand the definition file name relative to the directory of the file | ||
| 1283 | that contains the @code{declare-function} call. | ||
| 1284 | |||
| 1285 | You can also say that a function is defined by C code by specifying | ||
| 1286 | a file name ending in @samp{.c}. @code{check-declare-file} looks for | ||
| 1287 | these files in the C source code directory. This is useful only when | ||
| 1288 | you call a function that is defined only on certain systems. Most | ||
| 1289 | of the primitive functions of Emacs are always defined so they will | ||
| 1290 | never give you a warning. | ||
| 1275 | 1291 | ||
| 1276 | @node Function Safety | 1292 | @node Function Safety |
| 1277 | @section Determining whether a Function is Safe to Call | 1293 | @section Determining whether a Function is Safe to Call |