From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 17935BDCB for ; Thu, 8 Sep 2005 10:43:23 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j888hMQq008465 for ; Thu, 8 Sep 2005 10:43:22 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA03152 for ; Thu, 8 Sep 2005 10:43:21 +0200 (MET DST) Received: from mx2.mail.ru (mx2.mail.ru [194.67.23.122]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j888hLpM008458 for ; Thu, 8 Sep 2005 10:43:21 +0200 Received: from [193.219.28.149] (port=47498 helo=bely) by mx2.mail.ru with asmtp id 1EDI08-000FjY-00; Thu, 08 Sep 2005 12:43:20 +0400 Received: from localhost (HELO BELY) [127.0.0.1] by bely (0.0.0.2) with ESMTP (Classic Hamster Vr. 2.0 Build 2.0.0.1) ; Thu, 08 Sep 2005 12:47:04 +0400 X-Comment-To: Peter Jolly To: Peter Jolly Cc: caml-list Subject: Re: [Caml-list] camlp4 pa_macro References: <431ED6FB.9010009@jollys.org> From: Dmitry Bely Date: Thu, 08 Sep 2005 12:47:04 +0400 In-Reply-To: <431ED6FB.9010009@jollys.org> (Peter Jolly's message of "Wed, 07 Sep 2005 13:03:07 +0100") Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.5 (chayote, windows-nt) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Posting-Agent: Hamster/2.0.0.1 X-Miltered: at nez-perce with ID 431FF9AA.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 431FF9A9.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ifdef:01 ndebug:01 expr:01 expr:01 printf:01 printf:01 ndebug:01 debugging:01 cmo:01 cmo:01 syntax:01 ifdef:01 kprintf:01 fmt:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Peter Jolly writes: >> Is it possible to achieve with pa_macro something like this: >> >> IFDEF NDEBUG THEN >> DEFINE LOG(expr) = () >> ELSE >> DEFINE LOG(expr) = Printf.printf expr >> ENDIF >> ... >> LOG("x=%d,y=%d" x y); >> >> Unfortunately the code above does not work: debug version is OK, but then >> NDEBUG is turned on I have >> >> "This expression is not a function, it cannot be applied" on LOG() >> expression. > > Getting camlp4 to pretty-print the code after macro expansion is a > useful technique for debugging this sort of problem: > > $ camlp4 pa_o.cmo pa_op.cmo pr_o.cmo pa_macro.cmo test.ml > ... > Printf.printf ("x=%d,y=%d" x y) > > It should be clear why that isn't working. I see. Thank you very much for the info. > >> If I use >> >> LOG "x=%d,y=%d" x y; >> >> then the release version surprisingly works, but the debug one gives >> >> "Parse error: currified constructor" > > Yes, because this does not pass any arguments to the LOG macro - it > expands it with an empty . So this works in the latter case, > because LOG just expands to "Printf.printf", but in the former case you > end up with > > () "x=%d,y=%d" x y > > which is a syntax error as reported. > >> How to overcome this? > > IFDEF NDEBUG THEN > DEFINE LOG = Printf.kprintf ignore > ELSE > DEFINE LOG = Printf.printf > ENDIF It's quite useless - let debug = ref true let log fmt = if !debug then Printf.kprintf print_string fmt else Printf.kprintf ignore fmt will in fact give the same result. It does not solve the initial problem - completely remove the debugging code from the release binary. > Or just replace all instances of LOG with "if debug then Printf.printf", > on the grounds that the compiler is probably clever enough to prune > conditions that always evaluate to false, and you probably won't notice > any significant difference in speed even if it isn't. But the debugging code (format strings etc.) will be there. OK, the best I can get now is IFDEF NDEBUG THEN DEFINE LOG(expr) = () ELSE let dprintf = Printf.printf DEFINE LOG(expr) = expr ENDIF ... LOG(dprintf "x=%d,y=%d" x y); Not very elegant, but works. - Dmitry Bely