source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mandoc: add warning "cross reference to self"; inspired by mdoclint
@ 2017-07-02 15:32 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2017-07-02 15:32 UTC (permalink / raw)
  To: source

Log Message:
-----------
add warning "cross reference to self"; inspired by mdoclint

Modified Files:
--------------
    mandoc:
        main.c
        mandoc.1
        mandoc.h
        mandoc_xr.c
        mandoc_xr.h
        mdoc_validate.c
        read.c

Revision Data
-------------
Index: mandoc.1
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc.1,v
retrieving revision 1.210
retrieving revision 1.211
diff -Lmandoc.1 -Lmandoc.1 -u -p -r1.210 -r1.211
--- mandoc.1
+++ mandoc.1
@@ -1097,6 +1097,24 @@ The same standard section title occurs m
 .Pq mdoc
 A standard section header occurs in a section of the manual
 where it normally isn't useful.
+.It Sy "cross reference to self"
+.Pq mdoc
+An
+.Ic \&Xr
+macro refers to a name and section matching the section of the present
+manual page and a name mentioned in an
+.Ic \&Nm
+macro in the NAME or SYNOPSIS section, or in an
+.Ic \&Fn
+or
+.Ic \&Fo
+macro in the SYNOPSIS.
+Consider using
+.Ic \&Nm
+or
+.Ic \&Fn
+instead of
+.Ic \&Xr .
 .It Sy "unusual Xr order"
 .Pq mdoc
 In the SEE ALSO section, an
Index: mandoc_xr.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc_xr.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lmandoc_xr.h -Lmandoc_xr.h -u -p -r1.1 -r1.2
--- mandoc_xr.h
+++ mandoc_xr.h
@@ -19,12 +19,12 @@ struct	mandoc_xr {
 	struct mandoc_xr *next;
 	char		 *sec;
 	char		 *name;
-	int		  line;
+	int		  line;  /* Or -1 for this page's own names. */
 	int		  pos;
 	char		  hashkey[];
 };
 
 void		  mandoc_xr_reset(void);
-void		  mandoc_xr_add(const char *, const char *, int, int);
+int		  mandoc_xr_add(const char *, const char *, int, int);
 struct mandoc_xr *mandoc_xr_get(void);
 void		  mandoc_xr_free(void);
Index: mandoc.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc.h,v
retrieving revision 1.238
retrieving revision 1.239
diff -Lmandoc.h -Lmandoc.h -u -p -r1.238 -r1.239
--- mandoc.h
+++ mandoc.h
@@ -97,6 +97,7 @@ enum	mandocerr {
 	MANDOCERR_SEC_ORDER, /* sections out of conventional order: Sh title */
 	MANDOCERR_SEC_REP, /* duplicate section title: Sh title */
 	MANDOCERR_SEC_MSEC, /* unexpected section: Sh title for ... only */
+	MANDOCERR_XR_SELF,  /* cross reference to self: Xr name sec */
 	MANDOCERR_XR_ORDER, /* unusual Xr order: ... after ... */
 	MANDOCERR_XR_PUNCT, /* unusual Xr punctuation: ... after ... */
 	MANDOCERR_AN_MISSING, /* AUTHORS section without An macro */
Index: main.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/main.c,v
retrieving revision 1.296
retrieving revision 1.297
diff -Lmain.c -Lmain.c -u -p -r1.296 -r1.297
--- main.c
+++ main.c
@@ -763,8 +763,7 @@ parse(struct curparse *curp, int fd, con
 
 	if (man == NULL)
 		return;
-	if (curp->mmin < MANDOCERR_STYLE)
-		mandoc_xr_reset();
+	mandoc_xr_reset();
 	if (man->macroset == MACROSET_MDOC) {
 		if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
 			mdoc_validate(man);
@@ -816,7 +815,8 @@ parse(struct curparse *curp, int fd, con
 			break;
 		}
 	}
-	check_xr(file);
+	if (curp->mmin < MANDOCERR_STYLE)
+		check_xr(file);
 	mparse_updaterc(curp->mp, &rc);
 }
 
@@ -833,6 +833,8 @@ check_xr(const char *file)
 		manpath_base(&paths);
 
 	for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
+		if (xr->line == -1)
+			continue;
 		search.arch = NULL;
 		search.sec = xr->sec;
 		search.outkey = NULL;
Index: mandoc_xr.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc_xr.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lmandoc_xr.c -Lmandoc_xr.c -u -p -r1.1 -r1.2
--- mandoc_xr.c
+++ mandoc_xr.c
@@ -59,17 +59,18 @@ mandoc_xr_reset(void)
 	xr_first = xr_last = NULL;
 }
 
-void
+int
 mandoc_xr_add(const char *sec, const char *name, int line, int pos)
 {
-	struct mandoc_xr	 *xr;
+	struct mandoc_xr	 *xr, *oxr;
 	const char		 *pend;
 	size_t			  ssz, nsz, tsz;
 	unsigned int		  slot;
+	int			  ret;
 	uint32_t		  hv;
 
 	if (xr_hash == NULL)
-		return;
+		return 0;
 
 	ssz = strlen(sec) + 1;
 	nsz = strlen(name) + 1;
@@ -86,15 +87,21 @@ mandoc_xr_add(const char *sec, const cha
 	pend = xr->hashkey + tsz;
 	hv = ohash_interval(xr->hashkey, &pend);
 	slot = ohash_lookup_memory(xr_hash, xr->hashkey, tsz, hv);
-	if (ohash_find(xr_hash, slot) == NULL) {
+	if ((oxr = ohash_find(xr_hash, slot)) == NULL) {
 		ohash_insert(xr_hash, slot, xr);
 		if (xr_first == NULL)
 			xr_first = xr;
 		else
 			xr_last->next = xr;
 		xr_last = xr;
-	} else
-		free(xr);
+		return 0;
+	}
+
+	ret = (oxr->line == -1) ^ (xr->line == -1);
+	if (xr->line == -1)
+		oxr->line = -1;
+	free(xr);
+	return ret;
 }
 
 struct mandoc_xr *
Index: mdoc_validate.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mdoc_validate.c,v
retrieving revision 1.346
retrieving revision 1.347
diff -Lmdoc_validate.c -Lmdoc_validate.c -u -p -r1.346 -r1.347
--- mdoc_validate.c
+++ mdoc_validate.c
@@ -1104,6 +1104,8 @@ post_fname(POST_ARGS)
 	if ( ! (cp[0] == '\0' || (cp[0] == '(' && cp[1] == '*')))
 		mandoc_msg(MANDOCERR_FN_PAREN, mdoc->parse,
 		    n->line, n->pos + pos, n->string);
+	if (n->sec == SEC_SYNOPSIS && mdoc->meta.msec != NULL)
+		mandoc_xr_add(mdoc->meta.msec, n->string, -1, -1);
 }
 
 static void
@@ -1169,6 +1171,11 @@ post_nm(POST_ARGS)
 
 	n = mdoc->last;
 
+	if ((n->sec == SEC_NAME || n->sec == SEC_SYNOPSIS) &&
+	    n->child != NULL && n->child->type == ROFFT_TEXT &&
+	    mdoc->meta.msec != NULL)
+		mandoc_xr_add(mdoc->meta.msec, n->child->string, -1, -1);
+
 	if (n->last != NULL &&
 	    (n->last->tok == MDOC_Pp ||
 	     n->last->tok == MDOC_Lp))
@@ -2339,8 +2346,11 @@ post_xr(POST_ARGS)
 		    n->line, n->pos, "Xr %s", nch->string);
 	} else {
 		assert(nch->next == n->last);
-		mandoc_xr_add(nch->next->string, nch->string,
-		    nch->line, nch->pos);
+		if(mandoc_xr_add(nch->next->string, nch->string,
+		    nch->line, nch->pos))
+			mandoc_vmsg(MANDOCERR_XR_SELF, mdoc->parse,
+			    nch->line, nch->pos, "Xr %s %s",
+			    nch->string, nch->next->string);
 	}
 	post_delim(mdoc);
 }
Index: read.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/read.c,v
retrieving revision 1.184
retrieving revision 1.185
diff -Lread.c -Lread.c -u -p -r1.184 -r1.185
--- read.c
+++ read.c
@@ -139,6 +139,7 @@ static	const char * const	mandocerrs[MAN
 	"sections out of conventional order",
 	"duplicate section title",
 	"unexpected section",
+	"cross reference to self",
 	"unusual Xr order",
 	"unusual Xr punctuation",
 	"AUTHORS section without An macro",
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-07-02 15:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-02 15:32 mandoc: add warning "cross reference to self"; inspired by mdoclint schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).